类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
现在我可以得到一些函数名(字符串)如:"func1",如何通过这些函数名得到它对应的函数入口地址呢?即使一个指针变量p指向func1()。
网友回答:
如果是你的主程序中定义的函数,其实也是可以按照类似的方法从字符串调用的。
因为dlopen(filename,flag)这个函数比较特殊,如果filename是NULL,就是表示取得主程序的句柄。
一个例子:
#include <stdio.h>
#include <dlfcn.h>
void wel()
{
fputs("hello,wel!",stdout);
}
main()
{
void *handle=dlopen(NULL,RTLD_LAZY);
void (*fun)();
if (handle==NULL)
{
fputs(dlerror(),stderr);
exit(-1);
}
fun=dlsym(handle,"wel");
if (fun==NULL)
{
fputs(dlerror(),stderr);
exit(-2);
}
(*fun)();
dlclose(handle);
}