c++动态加载dll中的类(用于实现依据字符串类名创建对象)

    查看原文

参考资料:

http://blog.csdn.net/yysdsyl/archive/2008/07/08/2626033.aspx

用来生成dll的文件:

  1. Test.h  
  2.   
  3. class Test  
  4.   
  5. {  
  6.   
  7. public:  
  8.   
  9.     Test(void);  
  10.   
  11. public:  
  12.   
  13.     virtual ~Test(void);  
  14.   
  15. public:  
  16.   
  17.     virtual void DoSth()=0;  
  18.   
  19. };  

--------------------------------------------

 

  1. ///TTest.h  
  2.   
  3. /TTest继承自Test  
  4.   
  5. class TTest :  
  6.   
  7.     public Test  
  8.   
  9. {  
  10.   
  11. public:  
  12.   
  13.     TTest(void);  
  14.   
  15. public:  
  16.   
  17.     ~TTest(void);  
  18.   
  19. public:  
  20.   
  21.     virtual void DoSth(){std::cout<<"TTest:do sth/n";};  
  22.   
  23. };  
  1. ///关键  
  2.   
  3.   
  4.   
  5.   
  6.   
  7. extern "C" __declspec(dllexport) Test* CreateTTestPtr();  
  8.   
  9. extern "C" __declspec(dllexport) void DeleteTTestPtr(Test* t);  

 

--------------------------------------------------

  1. TTest.cpp  
  2.  
  3. #include "TTest.h"  
  4.   
  5.   
  6.   
  7. TTest::TTest(void)  
  8.   
  9. {  
  10.   
  11. }  
  12.   
  13.   
  14.   
  15. TTest::~TTest(void)  
  16.   
  17. {  
  18.   
  19.     std::cout<<"destruct TTest/n";  
  20.   
  21. }  
  22.   
  23.   
  24.   
  25. Test* CreateTTestPtr()  
  26.   
  27. {  
  28.   
  29.     return new TTest();  
  30.   
  31. }  
  32.   
  33.   
  34.   
  35. void DeleteTTestPtr(Test* t)  
  36.   
  37. {  
  38.   
  39.     if(t!=NULL)  
  40.   
  41.         delete t;  
  42.   
  43. }  
  1. 测试程序:  
  1. <pre class="csharp" name="code">#include "Test.h"  
  2.  
  3. #include <Windows.h>  
  4.   
  5.   
  6.   
  7. typedef Test* (CREATEFN)();  
  8.   
  9. typedef void (DELETEFN)(Test* );  
  10.   
  11.   
  12.   
  13.   
  14.   
  15. int _tmain(int argc, _TCHAR* argv[])  
  16.   
  17. {  
  18.   
  19.     HINSTANCE hd=::LoadLibrary("AnotherDll.dll");  
  20.   
  21.   
  22.   
  23.     CREATEFN* pfn;  
  24.   
  25.     DELETEFN* xfn;  
  26.   
  27.   
  28.   
  29.     pfn=(CREATEFN *)::GetProcAddress(hd,"CreateTTestPtr");  
  30.   
  31.     xfn=(DELETEFN *)::GetProcAddress(hd,"DeleteTTestPtr");  
  32.   
  33.       
  34.   
  35.     Test* t=(*pfn)();  
  36.   
  37.     t->DoSth();  
  38.   
  39.     (*xfn)(t);  
  40.   
  41.   
  42.   
  43.     getchar();  
  44.   
  45.     return 0;  
  46.   
  47. }  
  1.    
  1.    
  1.    
  1. -------------------------------------------------------------------  
  1.    
  1. 向dll中添加新的继承于Test基类时,实现自身的同时实现下面的两函数  
  1. extern "C" __declspec(dllexport) Test* CreateXXXPtr();  
  2.   
  3. extern "C" __declspec(dllexport) void DeleteXXXPtr(Test* t);  
  1.    
  1. (其中XXX表示新添加的类的类名,当然命名可以随意,只要你能在应用程序中找到这两导出函数,不过统一的命名规则  
  1. 对根据类名创建对象是有好处的) 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值