点击(此处)折叠或打开
-
//gcc -o test main.c -ldl
-
#include <stdio.h>
-
#include <string.h>
-
#include <dlfcn.h>
-
-
typedef long (*fLogin)(char *sUserName, char *sPassword);
-
typedef int (*ffoo)(char* buf, int len);
-
fLogin a = NULL;
-
ffoo b = NULL;
-
-
struct _s
-
{
-
unsigned* p;
-
char* fname;
-
} tmp[] =
-
{
-
{(unsigned*)&a, "howtoLoging"},
-
{(unsigned*)&b, "testfoo"}
-
};
-
-
int main(int argc, char* argv[])
-
{
-
void* flib = NULL;
-
const char* serror = NULL;
-
int i = 0;
-
-
flib = dlopen("testso.so", RTLD_NOW);
-
if(!flib)
-
{
-
printf("open lib testso.so fail:%sn", dlerror());
-
return -1;
-
}
-
-
for(i = 0; i < sizeof(tmp)/sizeof(struct _s);i++)
-
{
-
*tmp[i].p = (unsigned)dlsym(flib, tmp[i].fname);
-
if ((serror = dlerror()) != NULL)
-
{
-
printf("load %s fail:%s n", tmp[i].fname, serror);
-
dlclose(flib);
-
return -1;
-
}
-
}
-
-
a("admin", "123");
-
b("hello", strlen("hello"));
-
-
return 0;
- }
点击(此处)折叠或打开
-
//gcc -shared -o testso.so testso.c
-
#include <stdio.h>
-
-
long howtoLoging(char *sUserName, char *sPassword, int a)
-
{
-
printf("uname:%s, pass:%sn", sUserName, sPassword);
-
return (long)a;
-
}
-
-
int testfoo(char* buf, int len)
-
{
-
printf("len(%d):%cn", len, buf[0]) ;
-
return len;
- }