前幾天有人問我為什麼他引用了自己的類別到dll中後,一直出現Link錯誤,後來發現其實和VC設定有關係。請參考下圖:
為了維護方便,我們會將共用的程式碼至於同一專案中。以上面的範例來說,DLLWithDef會使用Common中的Student類別,並將method匯出讓DLLClient使用。假設這個method原型如下,讓我們看看該怎麼做。
Student* createStudent(string aName);
在DLLWithDef專案中,有幾件事情要做:
#include "../Common/Student.h" class DLLWITHOUTDEF_API CDLLWitDef { public: Student* createStudent(){ return new Student(); }; };
LIBRARY "DLLWithDef"
EXPORTS
createStudent
在DLLClient專案中,有幾件事情要做:
#include "../Common/Student.h" typedef Student* (*CreateStudent)(string name); LPCWSTR hwdll_def = W2T(L"DLLWithDef.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; }
友藏內心獨白: 替別人解決問題也可以順便整理筆記!