Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1814484
  • 博文数量: 274
  • 博客积分: 2366
  • 博客等级: 大尉
  • 技术积分: 1880
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-22 09:37
文章分类

全部博文(274)

文章存档

2022年(1)

2020年(10)

2019年(7)

2018年(18)

2017年(26)

2016年(32)

2015年(43)

2014年(30)

2013年(44)

2012年(36)

2011年(17)

2010年(10)

分类: C/C++

2013-03-06 15:27:36




点击(此处)折叠或打开

  1. //gcc -o test main.c -ldl
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <dlfcn.h>

  5. typedef long (*fLogin)(char *sUserName, char *sPassword);
  6. typedef int (*ffoo)(char* buf, int len);
  7. fLogin a = NULL;
  8. ffoo b = NULL;

  9. struct _s
  10. {
  11.     unsigned* p;
  12.     char* fname;
  13. } tmp[] =
  14. {
  15.     {(unsigned*)&a, "howtoLoging"},
  16.     {(unsigned*)&b, "testfoo"}
  17. };

  18. int main(int argc, char* argv[])
  19. {
  20.     void* flib = NULL;
  21.     const char* serror = NULL;
  22.     int i = 0;

  23.     flib = dlopen("testso.so", RTLD_NOW);
  24.     if(!flib)
  25.     {
  26.         printf("open lib testso.so fail:%sn", dlerror());
  27.         return -1;
  28.     }
  29.     
  30.     for(i = 0; i < sizeof(tmp)/sizeof(struct _s);i++)
  31.     {
  32.         *tmp[i].p = (unsigned)dlsym(flib, tmp[i].fname);
  33.         if ((serror = dlerror()) != NULL)
  34.         {
  35.             printf("load %s fail:%s n", tmp[i].fname, serror);
  36.             dlclose(flib);
  37.             return -1;
  38.         }
  39.     }
  40.     
  41.     a("admin", "123");
  42.     b("hello", strlen("hello"));
  43.     
  44.     return 0;
  45. }

点击(此处)折叠或打开

  1. //gcc -shared -o testso.so testso.c
  2. #include <stdio.h>

  3. long howtoLoging(char *sUserName, char *sPassword, int a)
  4. {
  5.     printf("uname:%s, pass:%sn", sUserName, sPassword);
  6.     return (long)a;
  7. }

  8. int testfoo(char* buf, int len)
  9. {
  10.     printf("len(%d):%cn", len, buf[0])    ;
  11.     return len;
  12. }




阅读(1135) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~