Problem
前幾天有人問我為什麼他引用了自己的類別到dll中後,一直出現Link錯誤,後來發現其實和VC設定有關係。請參考下圖:
為了維護方便,我們會將共用的程式碼至於同一專案中。以上面的範例來說,DLLWithDef會使用Common中的Student類別,並將method匯出讓DLLClient使用。假設這個method原型如下,讓我們看看該怎麼做。Student* createStudent(string aName);
How to?
DLL專案
在DLLWithDef專案中,有幾件事情要做:
- 新增method到匯出類別中,並include標頭檔。
#include "../Common/Student.h" class DLLWITHOUTDEF_API CDLLWitDef { public: Student* createStudent(){ return new Student(); }; };
- 將Student.cpp與Student.h透過加入現有項目的方式,加入到DLLWithDef中。
- 將method加入.def檔中,宣告匯出symbol。如果你使用裝載期間動態連結,就不用做這個動作。
LIBRARY "DLLWithDef" EXPORTS createStudent
Client專案
在DLLClient專案中,有幾件事情要做:
- 引用標頭檔並宣告method原型
#include "../Common/Student.h" typedef Student* (*CreateStudent)(string name); LPCWSTR hwdll_def = W2T(L"DLLWithDef.dll");
- 將Student.cpp與Student.h透過加入現有項目的方式,加入到DLLClient專案中。做法與DLL專案相同。
- 撰寫程式(測試用):
void testCreateStudent(HMODULE hModule){ CreateStudent fnCreateStudent = (CreateStudent)::GetProcAddress( hModule, "createStudent"); if( fnCreateStudent != NULL ){ Student* student = fnCreateStudent("test"); printf("Return = %x\n", student); delete student; } else { cout << "Init function failure!" << endl; } } int _tmain(int argc, _TCHAR* argv[]) { HMODULE hDefModule = ::LoadLibrary(hwdll_def); if( hDefModule != NULL ){ testCreateStudent(hDefModule); ::FreeLibrary(hDefModule); } else { cout << "Load " << hwdll_def << " failure!" << endl; } system("pause"); return 0; }
留言
張貼留言