AYE89

coding & learning

导航

用dlopen,dlsym加载动态链接库.so中函数

Posted on 2017-09-01 10:45  AYE89  阅读(6880)  评论(0编辑  收藏  举报

代码如下

static void *findSymbol(const char *path, const char *symbol) {
    void *handle = dlopen(path, RTLD_LAZY);
    if(!handle) {
        LOGE("handle %s is null", path);
        return NULL;
    }

    //Cydia::MSHookFunction(void *,void *,void **)
    void *target = dlsym(handle, symbol);
    if(!target) {
        LOGE("symbol %s is null", symbol);
    }
    return target;
}

void *target = findSymbol("libc.so", "__system_property_get");

dlopen打开失败:

dlopen第三方动态库时经常会发生打开失败的错误,可以适用dlerror()函数查看具体错误:

void *dlh;  
dlh = dlopen("libdes3-32.so", RTLD_NOW | RTLD_GLOBAL);  
if (dlh == NULL)  
{  
      printf("dlopen err:%s.\n",dlerror());  
}