QLibrary 动态加载外部库文件

原文地址::http://blog.csdn.net/fm0517/article/details/8166678


相关文章

1、qt调用.so库的问题----http://bbs.csdn.net/topics/390489760

2、QT源码解析(五)QLibrary跨平台调用动态库的实现----http://blog.csdn.net/tingsking18/article/details/5128307

3、QLibrary Class Reference(qt加载外部库)----http://no001.blog.51cto.com/1142339/282536/

4、QLibrary跨平台调用动态库的实现----http://blog.sina.com.cn/s/blog_5f853eb10100qfnh.html



[cpp]  view plain  copy
  1. /*转载请注明出处,谢谢*/  

背景:想搭一个软件系统,包含一个主模块,一堆子模块(用于扩展功能)。主模块是一个可执行程序,子模块为动态库,可以加载在主模块中。

问题:每添加一个扩展功能就要修改一下主模块的源程序,在其中添加头文件,lib文件,然后重新编译。很是麻烦。

目标:主模块读一个配置文件,根据其中的内容,动态加载子模块的DLL,以实现扩展功能灵活配置,而不需要每次都修改和重新编译主模块。


下面是个例子:

子模块代码:

[cpp]  view plain  copy
  1. extern "C" __declspec(dllexportint print2(int a, int b);  


[cpp]  view plain  copy
  1. int print2(int a, int b)  
  2. {  
  3.     printf("VideoDecoderDLL printint!\n");  
  4.     return (a+b);  
  5. }  
主模块代码:

[cpp]  view plain  copy
  1. #include <QLibrary>  
  2.   
  3. void Shell::Test()  
  4. {  
  5.     //m_vd->print();  
  6.     int *out=0;  
  7.     typedef int (*myfun)(intint);       // 定义导出函数类型    
  8.     QLibrary hdll( "I:/testDll/shell/VideoDecoderD.dll" );        //加载dll,当前目录    
  9.     if(hdll.load())    
  10.     {    
  11.         myfun fun1 = (myfun)hdll.resolve("print2");        //用resolve来解析fun1函数    
  12.         if ( fun1 )       //解析成功则进行运算并提示相关信息    
  13.         {    
  14.             printf("解析成功\n");  
  15.             printf("%d\n",fun1(2,3));    
  16.         }    
  17.         else  
  18.         {  
  19.             printf("解析失败\n");  
  20.         }  
  21.     }    
  22. }  


目前还没解决如何加载一个类,有知道办法的朋友欢迎留言。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值