dll导出函数调用封装

为了方便,备份

 

/********************************************************************
 created: 2012:24:4   12:22:40
 author:  Insert
 purpose: dll export functions encapsulation
*********************************************************************/
#ifndef __DLLEXPORT__H__F53BDD46_6C8D_4EB1_8E65_5ED75697F6B8__
#define __DLLEXPORT__H__F53BDD46_6C8D_4EB1_8E65_5ED75697F6B8__

#include <Windows.h>
#include <assert.h>

#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")

#define __CALLINGCONVENTION __cdecl

typedef struct _SNAMEPFUNC
{
 FARPROC*  pFunc;
 LPSTR    lpName;
} _SNF;

#define EMETHOD(__ret, __name, __pd, __pr)\
private:\
 __ret(__CALLINGCONVENTION *_fn##__name)__pd;\
public:\
 inline static __ret __name __pd\
 {\
  assert(NULL != _Instance()->_fn##__name);\
  return _Instance()->_fn##__name __pr;\
 }

#define BEGIN_EFUNC_MAP {\
 _SNF __snf[] ={

#define EFUNC(__name) {(FARPROC *)&_fn##__name, #__name},

#define END_EFUNC_MAP(__hm)\
 };\
 int __n = sizeof(__snf) / sizeof(_SNF);\
 for(int i = 0; i < __n; i++)\
 {\
 *__snf[i].pFunc = ::GetProcAddress( __hm, __snf[i].lpName );\
  assert(NULL != *__snf[i].pFunc && "Unknown export function!");\
 }\
}

class CExportFucntions
{
 //Interface of dll exports
 EMETHOD(void, funnoparam, (), ());
 EMETHOD(int, funwithparam, (int p1, bool p2), (p1, p2));
public:

private:
 CExportFucntions() : _hmModule(NULL)
 {
  _Instance();
 }
 ~CExportFucntions(){};

 static CExportFucntions* _Instance()
 {
  static CExportFucntions __ef;
  return &__ef;
 }

 const static TCHAR* _szModulePath;
 HMODULE _hmModule;

 BOOL _Initialize()
 {
  TCHAR szDllPath[MAX_PATH] = {0};
  if (0 == ::GetModuleFileName(NULL, szDllPath, MAX_PATH))
  {
   assert(0 && "GetModuleFileName failed!");
   return FALSE;
  }
  if (FALSE == ::PathAppend(szDllPath, _szModulePath))
  {
   assert(0 && "GetModuleFileName failed!");
   return FALSE;
  }
  if (FALSE == ::PathFileExists(szDllPath))
  {
   assert(0 && "DLL is not exist!");
   return FALSE;
  }

  _hmModule = ::LoadLibrary(szDllPath);
  if (NULL == _hmModule)
  {
   assert(0 && "LoadLibrary failed!");
   return FALSE;
  }

  BEGIN_EFUNC_MAP
   EFUNC(funnoparam)
   EFUNC(funwithparam)
  END_EFUNC_MAP(_hmModule)

  return TRUE;
 }

 void _UnInitialize()
 {
  if (NULL != _hmModule)
  {
   ::FreeLibrary(_hmModule);
  }
 }
};

const TCHAR* CExportFucntions::_szModulePath = TEXT("..\\somedll.dll");

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值