Qt DLL總結 二 建立及呼叫QT的 DLL

2021-09-01 03:21:54 字數 3947 閱讀 7232

目錄

qt dll總結【一】-鏈結庫預備知識

qt dll總結【二】-建立及呼叫qt的 dll

qt dll總結【三】-vs2008+qt 使用qpluginloader訪問dll

開發環境:vs2008+qt4.7.4

最近看了不少qt的dll例子,總結一下如何建立和呼叫qt 動態鏈結庫。

1、顯式鏈結dll,呼叫dll的全域性函式,採用qt的qlibrary方法

2、顯示鏈結dll,呼叫dll中類物件、成員函式。(通過物件即可實現類成員函式的呼叫)

①用虛函式表的方法,這也是com使用的方法,利用qt的qlibrary技術呼叫;

②用getprocaddress直接呼叫。

用qt的qpluginloader類直接呼叫生成的dll外掛程式類物件

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

前提:兩個專案檔案目錄

1、testdll專案:testdll_global.h,testdll.h,testdll.cpp

2、testmain exe應用專案:main.cpp

testdll_global.h 檔案源**一直不變

#ifndef testdll_global_h

#define testdll_global_h

#include #ifdef testdll_lib

# define testdll_export q_decl_export

#else

# define testdll_export q_decl_import

#endif

#endif // testdll_global_h

dll的顯式鏈結在某些時候比隱式鏈結具有更大的靈活性。比如,如果在執行時發現dll無法找到,程式可以顯示乙個錯誤資訊並能繼續執行。當你想為你的程式提供外掛程式服務時,顯式鏈結也很有用處

1、採用顯示鏈結呼叫dll中全域性函式,只需要乙個testdll.dll。

通常windows下程式顯示呼叫dll的步驟分為三步(三個函式):loadlibrary()、getprocadress()、freelibrary()

其中,loadlibrary() 函式用來載入指定的dll檔案,載入到呼叫程式的記憶體中(dll沒有自己的記憶體!)

getprocaddress() 函式檢索指定的動態鏈結庫(dll)中的輸出庫函式位址,以備呼叫

freelibrary() 釋放dll所佔空間 

testdll.dll專案中的testdll.h原始碼

#ifndef testdll_h

#define testdll_h

#include "testdll_global.h"

class testdll_export testdll

;extern "c" testdll_export void helloworld();

extern "c" testdll_export int add(int a,int b);

#endif // testdll_h

testdll.dll專案中的testdll.cpp原始碼

#include #include "testdll.h"

testdll::testdll()

testdll::~testdll()

void helloworld()

int add(int a,int b)

注: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

int main(int argc, char *argv)

;

extern "c" testdll_export testdll* gettestdll(); //獲取類testdll的物件

#endif // testdll_htestdll.cpp原始碼

#include #include "testdll.h"

testdll::testdll()

testdll::~testdll()

void testdll::helloworld()

testdll* gettestdll()

testmain專案中的main.cpp原始碼

typedef testdll* (*gettestdll)();//定義函式指標,獲取類testdll物件;

int main(int argc, char *argv)

} //載入失敗

else

std::cout << "dll is not loaded!"<

這個方法的使用得使用者可以很容易地為你的程式製作外掛程式。它的缺點是建立物件的記憶體必須在dll中分配

②用getprocaddress直接呼叫類物件中的成員函式

用qt的qpluginloader類直接呼叫生成的dll外掛程式類物件

這個方法,我單獨寫一篇總結,請看qpluginloader的簡單小例子vs2008+qt 使用qpluginloader訪問dll

3、採用隱式鏈結方法,通過qlibrary類對dll中類物件、全域性函式的呼叫

testdll.h

#ifndef testdll_h

#define testdll_h

#include "testdll_global.h"

class testdll_export testdll

;

extern "c" testdll_export int add(int a,int b); //自定義的外部函式

#endif // testdll_h

testdll.cpp原始碼

#include #include "testdll.h"

testdll::testdll()

testdll::~testdll()

void testdll::helloworld()

int add(int a,int b)

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原始碼

//引入testdll.lib檔案,和上面的2,3步工作同理

//#pragma comment(lib, "../debug/testdll.lib")

int main(int argc, char *argv)

結果即可編譯成功

QT呼叫QT DLL方法

qt提供了乙個 qlibrary 類供顯示呼叫 一 qt dll介面 extern c q decl export bool processdata float inputdata,int length,paramter param,sunshine retstructs,int retnum 二 ...

Qt中呼叫MatlabEngine總結

qt中呼叫matlab的方法 1.呼叫matlab封裝成的dll 2.呼叫matlab生成的exe 3.使用matlabengine。接下來,總結一下qt呼叫matlabengine的過程。qt呼叫matlabengine本質上是在qt中開啟matlab平台。首先,pro檔案中配置好engine.h...

C Builder建立及呼叫DLL

動態鏈結庫 dll 是windows程式設計常遇到的程式設計方法,下面我就介紹一下在 bcb c builder下簡稱bcb 中如何建立使用dll和一些技巧。一 建立 使用bcb file new建立乙個新的dll工程,並儲存好檔案bcb,生成乙個dll 的程式框架。1 dllentrypoint函...