Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120401
  • 博文数量: 95
  • 博客积分: 316
  • 博客等级: 二等列兵
  • 技术积分: 635
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-23 17:41
文章分类

全部博文(95)

文章存档

2014年(1)

2013年(1)

2012年(93)

我的朋友

分类: LINUX

2012-04-27 11:00:14

dlopen is the way to go. Here are a few examples:

Loading a plugin with dlopen:

#include        
...
int
main
(const int argc, const char *argv[])
{

 
char *plugin_name;
 
char file_name[80];
 
void *plugin;
 
...
  plugin
= dlopen (file_name, RTLD_NOW);
 
if (!plugin)
 
{
     fatal
("Cannot load %s: %s", plugin_name, dlerror ());
 
}

Compiling the above:

% cc  -ldl -o program program.o

Then, assuming this API for the plugins:

/* The functions we will find in the plugin */
typedef void (*init_f) ();
init_f init
;
typedef int (*query_f) ();
query_f query
;

Finding the address of init() in the plugin:

init = dlsym (plugin, "init");
result
= dlerror ();
if (result)
{
   fatal
("Cannot find init in %s: %s", plugin_name, result);
}
init
();

With the other function, query(), which returns a value:

query = dlsym (plugin, "query");
result
= dlerror ();
if (result)
{
    fatal
("Cannot find query in %s: %s", plugin_name, result);
}
printf
("Result of plugin %s is %d\n", plugin_name, query ());

You can retrieve the complete example on line.


C-Pluff, a plug-in framework for C

    


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