猿问

还有就是为什么:dlopen()报的错 ,但是明确的是可以找到SO文件?

#include "stdio.h"
#include "stdlib.h"
#include "dlfcn.h"

int main(void)
{
int (*So)(const char* pszTimestamp, const char* pszSDRType, const char* pszCallID,
const char* pszUserNo, const char* pszCallerNo, const char* pszCalledNo,
const char* pszOldNumber, const char* pszLocationID, const char* pszField1,
const char* pszField2, const char* pszField3, const char* pszField4);
const char *errmsg = "";

printf("begin!\n");

void *SoLib = dlopen("/root/RecordDll//Release/libRecordDll.so", RTLD_LAZY);

printf("111");

if (NULL == SoLib)
{
printf("222");
fprintf(stderr, "Failed to load libRecordDll.so:%s\n", dlerror());
return 1;
}

dlerror();

printf("333");

*(void **)(&So) = dlsym(SoLib, "GetInfo");

printf("444");

if (NULL != (errmsg = dlerror()))
{
printf("%s\n", errmsg);
return 1;
}

printf("555");

int ret = So("2011-11-11 11:11:11", "8", "11",
"111", "1111", "11111",
"111111", "1111111", "1~1~1",
"2011-11-11 11:11:11~2012-11-11 11:11:11~0~03", "", "");

printf("%d", ret);

dlclose(SoLib);
return 0;

}

拉风的咖菲猫
浏览 170回答 2
2回答

Smart猫小萌

你这段代码应该没问题,我那libstdc++试过了,没错。问题应该在你的/root/RecordDll//Release/libRecordDll.so,这个动态库GetInfo函数有bug。指针错误或者数据越界。我这代码:#include <stdio.h>#include <stdlib.h>#include <dlfcn.h>int main(void){// /usr/lib/libstdc++.so.6int (*mygetchar)(void);const char *errmsg = "";printf("begin!\n");void *SoLib = dlopen("/usr/lib/libstdc++.so.6", RTLD_LAZY);printf("111\n");if (NULL == SoLib){printf("222\n");fprintf(stderr, "Failed to load libstdc++.so.6:%s\n", dlerror());return 1;}dlerror();printf("333\n");*(void **)(&mygetchar) = dlsym(SoLib, "getchar");//printf("444\n");if (NULL != (errmsg = dlerror())){printf("%s\n", errmsg);return 1;}printf("555\n");int ret = mygetchar();printf("%d\n", ret);dlclose(SoLib);return 0;}上面代码跑没问题的,你的要错就是在动态库本身了。“还有就是:dlopen()报的错 ,但是明确的是可以找到SO文件,”/root/RecordDll//Release/libRecordDll.so看到了,/RecordDll//Release这中间多了一根右斜杆,路径错误,找到就见鬼了。

猛跑小猪

应该是有个指针指错了
随时随地看视频慕课网APP
我要回答