標籤:
一般的DLL匯出類方法,一個簡單的例子:
dllExample.h:
1 #pragma once 2 3 #ifdef DLL_EXPORTS 4 #define DLL_API __declspec(dllexport) 5 #else 6 #define DLL_API __declspec(dllimport) 7 #endif 8 9 class DLL_API ExportClass10 {11 pirvate:12 int x;13 public:14 void foo();15 };
dllExample.cpp:
1 #define DLL_EXPORTS2 #include "dllExample.h"3 4 void ExportClass::foo()5 {6 //do something...7 return;8 }
而外部代碼只需要包含標頭檔,就會自動匯入ExportClass的定義。
編譯時間串連該DLL對應的lib,運行時提供DLL檔案,即可正常運行。
不過這種簡單的DLL匯出存在一個限制,如果我們匯出的類中含有非C++基礎類型:
dllExample.h:
1 #pragma once 2 3 #ifdef DLL_EXPORTS 4 #define DLL_API __declspec(dllexport) 5 #else 6 #define DLL_API __declspec(dllimport) 7 #endif 8 9 class DLL_API ExportClass10 {11 pirvate:12 std::string x; //此處的string類型匯出是不安全的13 public:14 void foo();15 };
我們知道, 對於STL,微軟為每個版本的VS都有不同的實現,VS2008(VC90),VS2010(VC100),VS2013(VC120)。
由於不同的STL的實現,我們不能在不同的版本見直接傳遞std::string, 否則運行期可能出現不可預知的錯誤。
而事實上我們在ExportClass中的std::string x變數是不希望被外部直接使用的,也就是並沒有export的必要,事實上,不建議讓dll向外匯出任何關於非C++基礎類型的定義。
但是由於ExportClass需要向外匯出(因為需要使用foo()函數),應該如何處理這樣的矛盾呢?
對於這樣的問題,我們需要使用C++的抽象類別(其實就是java中的interface概念)來解決:
我們需要:
1. 申明一個只有純虛函數和C++基礎類型的基類,所有需要向外部匯出的定義都包含在該類中。
2. 申明另一個類,繼承該基類。
3. 實現一個返回基類函數指標的getInstance函數,即返回一個衍生類別執行個體的Factory 方法。
4. 在外部代碼中,通過多態機制訪問該類。
dllExample.h:
1 #pragma once 2 3 #ifdef DLL_EXPORTS 4 #define DLL_API __declspec(dllexport) 5 #else 6 #define DLL_API __declspec(dllimport) 7 #endif 8 9 class DLL_API ExportInterface10 {11 public:12 virtual void foo() = 0;13 };14 15 extern "C" DLL_API ExportInterface* getInstance();16 17 #ifdef DLL_EXPORTS //我們並不需要向外匯出該類的定義,在外部代碼編譯時間,也不需要包含此類的定義。18 class ExportClass: public ExportInterface19 {20 pirvate:21 std::string x; //由於外部代碼對此不可見,此處的std::string是安全的。22 public:23 void foo(); //函數體在dllExample.cpp中實現24 };25 #endif
dllExample.cpp:
#define DLL_EXPORTS#include "dllExample.h"extern "C" DLL_API ExportInterface* getInstance(){ ExportInterface* pInstance = new ExportClass(); return pInstance;}void ExportClass::foo(){ //do something... return;}
淺談Windows中DLL匯出類