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

时间:2022-03-28 15:52:14

代码如下

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());
}