Qt动态库和静态库的加载及使用

一、动态链接库调用方式

1、动态调用 Run-time Dynamic Linking

动态调用主要通过API函数(Window LoadLibrary、GetProcAddress、FressLibrary)来调用程序运行后需要的DLL函数,节省内存空间。QT中,则主要通过QLibrary进行动态加载(适用于跨平台)。

2、静态调用 Load-time Dynamic Linking

静态调用前提是在编译之前已经明确知道调用DLL中的哪些函数,需要lib和相应的导入头文件*.h。编译时,在目标文件中只保留必要的链接信息,而不含DLL函数的代码;当程序执行时,利用链接信息加载DLL函数代码并在内存中将其连接入调用程序的执行空间中,其主要目的是便于代码共享。

二、QT 5.0调用CVI生成的动态链接库实例

1、CVI生成动态链接库

CVI是个很强大的开发工具
创建过程参考
http://wenku.baidu.com/link?url=hvST1qn3UTjIYvLimf2cSDovK5YCagHU--WjRKOqXlYplBV-MXEGS-jqikiZlnRTNh3GM4efyFy_GSqGC26QMZWN5UUBkUSmeV3uJPXrBj3
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 代码dll1.h和dll1.c  
  2. #ifndef __dll1_H__  
  3. #define __dll1_H__  
  4.   
  5. #include "cvidef.h"  
  6. int __stdcall Add(intint);  
  7.   
  8. #endif  /* ndef __dll1_H__ */  






[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <cvirte.h>  
  2. #include "dll1.h"  
  3.   
  4. int __stdcall DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)  
  5. {  
  6.     switch (fdwReason)  
  7.     {  
  8.         case DLL_PROCESS_ATTACH:  
  9.             if (InitCVIRTE (hinstDLL, 0, 0) == 0)  
  10.                 return 0;     /* out of memory */  
  11.             break;  
  12.         case DLL_PROCESS_DETACH:  
  13.             CloseCVIRTE ();  
  14.             break;  
  15.     }  
  16.       
  17.     return 1;  
  18. }  
  19.   
  20. int __stdcall DllEntryPoint (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)  
  21. {  
  22.     /* Included for compatibility with Borland */  
  23.   
  24.     return DllMain (hinstDLL, fdwReason, lpvReserved);  
  25. }  
  26.   
  27. /*extern "C" */int __stdcall Add(int x, int y)  
  28. {  
  29.     return x+y;  
  30. }  


2、QT动态链接库的调用

2.1 动态调用

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "mydlg.h"  
  2. #include "ui_mydlg.h"  
  3. #include "qmessagebox.h"  
  4. #include <QLibrary>  
  5.   
  6. MyDlg::MyDlg(QWidget *parent) :  
  7.     QDialog(parent),  
  8.     ui(new Ui::MyDlg)  
  9. {  
  10.     ui->setupUi(this);  
  11.   
  12. }  
  13.   
  14. MyDlg::~MyDlg()  
  15. {  
  16.     delete ui;  
  17. }  
  18.   
  19. void MyDlg::on_pushButton_clicked()  
  20. {  
  21.      int a = 10;  
  22.      int b = 11;  
  23.      int c = 0;  
  24.      typedef int (*myfun)(int ,int);  
  25.      QLibrary hdll("dll1.dll");  
  26.      if(hdll.load())  
  27.      {  
  28.          QMessageBox::about(this"Msg","Load Success!");  
  29.          myfun fun1 = (myfun)hdll.resolve("Add");  
  30.          if(fun1)  
  31.          {  
  32.              c = fun1(a, b);  
  33.              QString result_msg = tr("dll加载成功!")+QString::number(c,10);  
  34.              QMessageBox::about(this"Result""The Sum is "+result_msg);  
  35.   
  36.          }  
  37.          hdll.unload();  
  38.      }  
  39. }  




2.2 静态调用

参考http://hi.baidu.com/ayuyuan/item/756788382b4c95573075a1c6
在XX.pro中添加: LIBS += ./debug/dll1.lib
创建并添加头文件:dll2.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //dll2.h  
  2. #ifndef DLL2_H  
  3. #define DLL2_H  
  4. //#pragma comment(lib, "dll1.lib")  
  5.   
  6. extern "C" __declspec(dllimportint __stdcall Add(intint);  
  7. #endif // DLL1_H  
  8.   
  9.   
  10.   
  11.   
  12.   
  13.   
  14.   
  15. //myDlg.cpp中直接使用DLL函数  
  16. void MyDlg::on_pushButton_clicked()  
  17. {  
  18.      int a = 10;  
  19.      int b = 11;  
  20.      int c = 0;  
  21.   
  22.      c = Add(a, b);  
  23.      QString result_msg = tr("dll加载成功!")+QString::number(c,10);  
  24.      QMessageBox::about(this"Result""The Sum is "+result_msg);  
  25.   
  26. }  
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值