//获取函数的信息 //dladdr() returns 0 on error, and nonzero on success //If no symbol matching addr could be found, then dli_sname and dli_saddr are set to NULL intdladdr(void *addr, Dl_info *info); The function dladdr() takes a function pointer and tries to resolve name and file where it is located. Information is stored in the Dl_info structure: typedef struct { constchar *dli_fname; /* Pathname of shared object that contains address */ void *dli_fbase; /* Address at which shared object is loaded */ constchar *dli_sname; /* Name of nearest symbol with address lower than addr */ void *dli_saddr; /* Exact address of symbol named in dli_sname */ } Dl_info; //类似dlsym() void *dlvsym(void *handle, char *symbol, char *version);
//方法2,相当与Windows下的 GetProcAddress() 函数. pf=(PFUN)dlsym(handle,"sub"); if(pf!=NULL){ //获取函数地址信息 Dl_info info; int r= dladdr((void*)pf,&info); printf("library filepath:%s\n"\ "address1:0x%08x\n"\ "funciton name:%s\n"\ "address2:0x%08x\n\n", info.dli_fname,info.dli_fbase, info.dli_sname,info.dli_saddr); //0 表示失败 if(r==0){ printf("can not get the dl info...\n"); } printf("get the function address...\n"); //调用函数 printf("result: %d \n",pf(50,15)); }else{ printf("can not get the function address...\n"); } //卸载链接库 dlclose(handle); return0; }