參考資料:
http://blog.csdn.net/yysdsyl/archive/2008/07/08/2626033.aspx
用來產生dll的檔案:
////////////////////////////Test.hclass Test{public: Test(void);public: virtual ~Test(void);public: virtual void DoSth()=0;};
--------------------------------------------
///////////////////TTest.h/////////////TTest繼承自Testclass TTest : public Test{public: TTest(void);public: ~TTest(void);public: virtual void DoSth(){std::cout<<"TTest:do sth/n";};};
///////////關鍵extern "C" __declspec(dllexport) Test* CreateTTestPtr();extern "C" __declspec(dllexport) void DeleteTTestPtr(Test* t);
--------------------------------------------------
////////////////TTest.cpp#include "TTest.h"TTest::TTest(void){}TTest::~TTest(void){ std::cout<<"destruct TTest/n";}Test* CreateTTestPtr(){ return new TTest();}void DeleteTTestPtr(Test* t){ if(t!=NULL) delete t;}
測試程式:
#include "Test.h"#include <Windows.h>typedef Test* (CREATEFN)();typedef void (DELETEFN)(Test* );int _tmain(int argc, _TCHAR* argv[]){ HINSTANCE hd=::LoadLibrary("AnotherDll.dll"); CREATEFN* pfn; DELETEFN* xfn; pfn=(CREATEFN *)::GetProcAddress(hd,"CreateTTestPtr"); xfn=(DELETEFN *)::GetProcAddress(hd,"DeleteTTestPtr"); Test* t=(*pfn)(); t->DoSth(); (*xfn)(t); getchar(); return 0;}
-------------------------------------------------------------------
向dll中添加新的繼承於Test基類時,實現自身的同時實現下面的兩函數
extern "C" __declspec(dllexport) Test* CreateXXXPtr();extern "C" __declspec(dllexport) void DeleteXXXPtr(Test* t);
(其中XXX表示新添加的類的類名,當然命名可以隨意,只要你能在應用程式中找到這兩匯出函數,不過統一的命名規則
對根據類名建立對象是有好處的)