Qt DLL總結【二】-創建及調用QT的 DLL(三篇)good


目錄

Qt DLL總結【一】-鏈接庫預備知識

Qt DLL總結【二】-創建及調用QT的 DLL  

Qt DLL總結【三】-VS2008+Qt 使用QPluginLoader訪問DLL

開發環境:VS2008+Qt4.7.4

 

最近看了不少Qt的DLL例子,總結一下如何創建和調用QT 動態鏈接庫。

 

先講一下對QT動態鏈接庫的調用方法,主要包括:

1、顯式鏈接DLL,調用DLL的全局函數,采用Qt的QLibrary方法

2、顯示鏈接DLL,調用DLL中類對象、成員函數。(通過對象即可實現類成員函數的調用)

 

①用虛函數表的方法,這也是COM使用的方法,利用Qt的QLibrary技術調用;

②用GetProcAddress直接調用。

用Qt的QPluginLoader類直接調用生成的DLL插件類對象

3、隱式鏈接DLL:也是采用Qt的Qlibrary方法

關於這種三種方法,下面詳細敘說

 

詳細分類敘述

 

前提:兩個項目文件目錄

1、TestDLL項目:testdll_global.h,TestDll.h,TestDll.cpp

2、TestMain exe應用項目:main.cpp

 

testdll_global.h 文件源代碼一直不變

 

Cpp代碼  
  1. #ifndef TESTDLL_GLOBAL_H  
  2. #define TESTDLL_GLOBAL_H  
  3.   
  4. #include <QtCore/qglobal.h>  
  5.   
  6. #ifdef TESTDLL_LIB  
  7. # define TESTDLL_EXPORT Q_DECL_EXPORT  
  8. #else  
  9. # define TESTDLL_EXPORT Q_DECL_IMPORT  
  10. #endif  
  11.   
  12. #endif // TESTDLL_GLOBAL_H  

 

      DLL的顯式鏈接在某些時候比隱式鏈接具有更大的靈活性。比如,如果在運行時發現DLL無法找到,程序可以顯示一個錯誤信息並能繼續運行。當你想為你的程序提供插件服務時,顯式鏈接也很有用處

 

1、采用顯示鏈接調用DLL中全局函數,只需要一個TestDLL.dll。

        通常Windows下程序顯示調用dll的步驟分為三步(三個函數):LoadLibrary()、GetProcAdress()、FreeLibrary()

        其中,LoadLibrary() 函數用來載入指定的dll文件,加載到調用程序的內存中(DLL沒有自己的內存!)

         GetProcAddress() 函數檢索指定的動態鏈接庫(DLL)中的輸出庫函數地址,以備調用

         FreeLibrary() 釋放dll所占空間 

      而QT的QLibrary類顯示鏈接調用DLL的步驟:load()、resolve(const char * symbol )、unload()和VC步驟類似

 

TestDll.dll項目中的TestDLL.h源碼

 

Cpp代碼  
  1. #ifndef TESTDLL_H  
  2. #define TESTDLL_H  
  3.   
  4. #include "testdll_global.h"  
  5.   
  6. class TESTDLL_EXPORT TestDll  
  7. {  
  8. public:  
  9.     TestDll();  
  10.     ~TestDll();   
  11. private:  
  12.   
  13.   
  14. };  
  15. extern "C" TESTDLL_EXPORT void helloWorld();       
  16. extern "C" TESTDLL_EXPORT int add(int a,int b);    
  17. #endif // TESTDLL_H  

 

TestDll.dll項目中的TestDLL.cpp源碼

 

Cpp代碼  
  1. #include <iostream>  
  2. #include "TestDll.h"  
  3.   
  4. TestDll::TestDll()  
  5. {  
  6.   
  7. }  
  8.   
  9. TestDll::~TestDll()  
  10. {  
  11.   
  12. }  
  13.   
  14. void helloWorld()  
  15. {  
  16.     std::cout << "hello,world!";  
  17. }  
  18. int add(int a,int b)  
  19. {  
  20.     return a + b;  
  21. }  

   注:1)建立成功DLL項目后,可以在VS命令提示行中用命令"dumpbin -exports DllTest.dll"來查看(也可以用VC工具包中的depends使用程序來查看)  
   注:2)必須使用extern "C"鏈接標記,否則C++編譯器會產生一個修飾過的函數名,這樣導出函數的名字將不再是helloworld,而是一個形如" ?helloWorld@TestDll@@UAEXXZ”的名字。為什么名字不是helloworld呢?這是因為C++為了支持函數的重載,會在編譯時將函數的參數類型信息以及返回值類型信息加入到函數名中,這樣代碼中名字一樣的重載函數,在經過編譯后就互相區分開了,調用時函數名也經過同樣的處理,就能找到對應的函數了。詳細可以看這篇文章動態鏈接庫(Dynamic Link Library)學習筆記

 

 TestMain項目 main.cpp

 

Cpp代碼  
  1. #include <QtCore/QCoreApplication>  
  2. #include <iostream>  
  3. #include <QLibrary>  
  4.   
  5. typedef int (*Fun)(int,int); //定義函數指針,int add(int a,int b);      
  6. int main(int argc, char *argv[])  
  7. {  
  8.     QCoreApplication a(argc, argv);  
  9.       
  10.     QLibrary mylib("TestDll.dll");   //聲明所用到的dll文件  
  11.     int result;  
  12.     //判斷是否正確加載  
  13.     if (mylib.load())                
  14.         {  
  15.             std::cout << "DLL load is OK!"<<std::endl;  
  16.             //調用外部函數 add()  
  17.             Fun add = (Fun)mylib.resolve("add");     
  18.             //是否成功連接上 add() 函數  
  19.             if (add)                    
  20.                 {  
  21.                     std::cout << "Link to add Function is OK!"<<std::endl;  
  22.                      //這里函數指針調用dll中的 add() 函數  
  23.                     result = add(5,6);       
  24.                     std::cout << result;  
  25.                 }  
  26.             else  
  27.                 std::cout << "Link to add Function failed!!"<<std::endl;  
  28.   
  29.               
  30.     }  
  31.     //加載失敗  
  32.     else  
  33.         std::cout << "DLL is not loaded!"<<std::endl;  
  34.        
  35.   
  36.     return a.exec();  
  37. }   

2、采用顯示鏈接,調用C++類中的類對象、成員函數 

     如果你想導出並顯式鏈接一組C++類中的成員函數又該怎么辦呢?這里有兩個問題。第一是C++成員函數名是經過修飾的(即使指定extern "C"標記也是這樣);第二是C++不允許將指向成員函數的指針轉換成其它類型。這兩個問題限制了C++類的顯式鏈接。下面介紹兩種方法來解決這個問題:

①用虛函數表的方法,這也是COM使用的方法,利用Qt的QLibrary技術調用;

②用GetProcAddress直接調用。

用Qt的QPluginLoader類直接調用生成的DLL插件類對象

     ①虛函數表的方法,QLibrary 技術調用

TestDll.h代碼

 

Cpp代碼  
  1. #ifndef TESTDLL_H  
  2. #define TESTDLL_H  
  3.   
  4. #include "testdll_global.h"  
  5.   
  6. class TESTDLL_EXPORT TestDll  
  7. {  
  8. public:  
  9.     TestDll();  
  10.     virtual~TestDll();    
  11.     virtual void helloWorld(); //類成員函數  
  12. private:  
  13.   
  14.   
  15. };     
  16. extern "C" TESTDLL_EXPORT TestDll* getTestDll(); //獲取類TestDll的對象  
  17. #endif // TESTDLL_H  

 

 TestDll.cpp源碼

 

Cpp代碼  
  1. #include <iostream>  
  2. #include "TestDll.h"  
  3.   
  4. TestDll::TestDll()  
  5. {  
  6.   
  7. }  
  8.   
  9. TestDll::~TestDll()  
  10. {  
  11.   
  12. }  
  13.   
  14. void TestDll::helloWorld()  
  15. {  
  16.     std::cout << "hello,world!";  
  17. }  
  18.   
  19. TestDll* getTestDll()  
  20. {  
  21.     return new TestDll();  
  22. }  

 

 TestMain項目中的main.cpp源碼

 

Cpp代碼  
  1. #include <QtCore/QCoreApplication>  
  2. #include <iostream>  
  3. #include <QLibrary>  
  4. #include "../TestDll/TestDll.h"  //頭文件還是需要加的,否則無法解析TestDll類  
  5. typedef TestDll* (*GetTestDll)();//定義函數指針,獲取類TestDLL對象;    
  6. int main(int argc, char *argv[])  
  7. {  
  8.     QCoreApplication a(argc, argv);  
  9.   
  10.     QLibrary mylib("TestDll.dll");   //聲明所用到的dll文件  
  11.     int result;  
  12.     //判斷是否正確加載  
  13.     if (mylib.load())                
  14.         {  
  15.             GetTestDll getTestDll = (GetTestDll)mylib.resolve("getTestDll");  
  16.             if(getTestDll)  
  17.             {  
  18.                 TestDll *testDll = getTestDll();  
  19.                 testDll->helloWorld();  
  20.                 delete testDll;  
  21.             }  
  22.     }  
  23.     //加載失敗  
  24.     else  
  25.         std::cout << "DLL is not loaded!"<<std::endl;  
  26.     return a.exec();  
  27. }  

        這個方法的使用得用戶可以很容易地為你的程序制作插件。它的缺點是創建對象的內存必須在dll中分配

 

 

②用GetProcAddress直接調用類對象中的成員函數

這個方法,我沒測試,對我沒對大作用,還得用def導出DLL函數,有興趣的就參考一下這篇文章。DLL中類的顯式鏈接

        ③用Qt的QPluginLoader類直接調用生成的DLL插件類對象

           這個方法,我單獨寫一篇總結,請看QPluginLoader的簡單小例子VS2008+Qt 使用QPluginLoader訪問DLL

 

3、采用隱式鏈接方法,通過QLibrary類對DLL中類對象、全局函數的調用

 

TestDll.h

 

Cpp代碼  
  1. #ifndef TESTDLL_H  
  2. #define TESTDLL_H  
  3.   
  4. #include "testdll_global.h"  
  5.   
  6. class TESTDLL_EXPORT TestDll  
  7. {  
  8. public:  
  9.     TestDll();  
  10.     ~TestDll();   
  11.     void helloWorld(); //類成員函數  
  12. private:  
  13.   
  14.   
  15. };     
  16. extern "C" TESTDLL_EXPORT int add(int a,int b);  //自定義的外部函數  
  17. #endif // TESTDLL_H  

TestDll.cpp源碼

Cpp代碼  
  1. #include <iostream>  
  2. #include "TestDll.h"  
  3.   
  4. TestDll::TestDll()  
  5. {  
  6.   
  7. }  
  8.   
  9. TestDll::~TestDll()  
  10. {  
  11.   
  12. }  
  13.   
  14. void TestDll::helloWorld()  
  15. {  
  16.     std::cout << "hello,world!";  
  17. }  
  18. int add(int a,int b)  
  19. {  
  20.     return a + b;  
  21. }  

 

TestMain項目中的main.cpp ,需要稍微配置頭文件和lib文件

1、在項目中主程序引入TestDll.h頭文件,

2、配置項目屬性:加入TestDLL.lib的文件目錄,在Linker/General/Additional Library Diretories里面選擇TestDll.lib的文件目錄D:\VSWorkSpace\Test\Debug

3、配置項目屬性:加入TestDll.lib文件,在Linker/Input/Additional Dependencies 中加入 TestDll.lib

 

main.cpp源碼

Cpp代碼  
  1. #include <QtCore/QCoreApplication>  
  2. #include <iostream>  
  3. #include <QLibrary>  
  4. #include "../TestDll/TestDll.h"  
  5. //引入TestDll.lib文件,和上面的2,3步工作同理  
  6. //#pragma comment(lib, "../Debug/TestDll.lib")  
  7. int main(int argc, char *argv[])  
  8. {  
  9.     QCoreApplication a(argc, argv);  
  10.     int result = add(5,6);  
  11.     std::cout << result;  
  12.     TestDll dll;  
  13.     dll.helloWorld();  
  14.         return a.exec();  
  15. }  

 結果即可編譯成功

 

http://qimo601.iteye.com/blog/1397936


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM