一种动态方式调用dll中类

IKPerson.h

[cpp]  view plain copy
  1. #ifndef _IKPERSON_H_  
  2. #define _IKPERSON_H_  
  3. #ifdef DLL_EXPORT  
  4. #define DLL_API extern "C" __declspec(dllexport)  
  5. #else  
  6. #define DLL_API extern "C" __declspec(dllimport)  
  7. #endif  
  8. /* 
  9.     设计这个接口类的作用: 
  10.     能采用动态调用方式使用这个类 
  11. */  
  12. class IKPerson  
  13. {  
  14. public:  
  15.     virtual ~IKPerson(void//对于基类,显示定义虚析构函数是个好习惯(注意,为什么请google)  
  16.     {  
  17.     }  
  18.     virtual int GetOld(voidconst = 0;  
  19.     virtual void SetOld(int nOld) = 0;  
  20.     virtual const char* GetName(voidconst = 0;  
  21.     virtual void SetName(const char* szName) = 0;  
  22. };  
  23. /* 导出函数声明 */  
  24. DLL_API IKPerson* _cdecl GetIKPerson(void);  
  25. typedef IKPerson* (__cdecl *PFNGetIKPerson)(void);  
  26. #endif  

 

KChinese.h

[cpp]  view plain copy
  1. #pragma once  
  2. #define DLL_EXPORT  
  3. #include "ikperson.h"  
  4. class CKChinese :  
  5.     public IKPerson  
  6. {  
  7. public:  
  8.     CKChinese(void);  
  9.     ~CKChinese(void);  
  10.     virtual int GetOld(voidconst;  
  11.     virtual void SetOld(int nOld);  
  12.     virtual const char* GetName(voidconst;  
  13.     virtual void SetName(const char* szName);  
  14. private:  
  15.     int m_nOld;  
  16.     char m_szName[64];  
  17. };  

KChinese.cpp

[cpp]  view plain copy
  1. #include "StdAfx.h"  
  2. #include "KChinese.h"  
  3. CKChinese::CKChinese(void) : m_nOld(0)  
  4. {  
  5.     memset(m_szName, 0, 64);  
  6. }  
  7. CKChinese::~CKChinese(void)  
  8. {  
  9. }  
  10. int CKChinese::GetOld(voidconst  
  11. {  
  12.     return m_nOld;  
  13. }  
  14. void CKChinese::SetOld(int nOld)  
  15. {  
  16.     this->m_nOld = nOld;  
  17. }  
  18. const char* CKChinese::GetName(voidconst  
  19. {  
  20.     return m_szName;  
  21. }  
  22. void CKChinese::SetName(const char* szName)  
  23. {  
  24.     strncpy(m_szName, szName, 64);  
  25. }  
  26. /* 导出函数定义 */  
  27. IKPerson* __cdecl GetIKPerson(void)  
  28. {  
  29.     IKPerson* pIKPerson = new CKChinese();  
  30.     return pIKPerson;  
  31. }  

 

调用代码

callclassExportDll.cpp

[cpp]  view plain copy
  1. // callclassExportDll.cpp : Defines the entry point for the console application.  
  2. //  
  3. #include "stdafx.h"  
  4. #include <iostream>  
  5. using namespace std;  
  6. #include "../../classExportDll/classExportDll/IKPerson.h"  
  7. int _tmain(int argc, _TCHAR* argv[])  
  8. {  
  9.     HMODULE hDll = ::LoadLibrary(_T("classExportDll.dll"));  
  10.     if (NULL != hDll)  
  11.     {  
  12.         PFNGetIKPerson pFun = (PFNGetIKPerson)::GetProcAddress(hDll, "GetIKPerson");  
  13.         if (NULL != pFun)  
  14.         {  
  15.             IKPerson* pIKPerson = (*pFun)();  
  16.             if (NULL != pIKPerson)  
  17.             {  
  18.                 pIKPerson->SetOld(103);  
  19.                 pIKPerson->SetName("liyong");  
  20.                 cout << pIKPerson->GetOld() << endl;  
  21.                 cout << pIKPerson->GetName() << endl;  
  22.                 delete pIKPerson;             
  23.             }  
  24.         }  
  25.         ::FreeLibrary(hDll);  
  26.     }  
  27.     return 0;  
  28. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值