用宏定义封装LoadLibrary,方便的动态加载dll

同学们动态加载dll的时候是不是感觉挺麻烦的,每次都::LoadLibrary,::GetProcAddress,还要typedef一堆函数。最近闲来无聊,用宏封装了一下,可以少写不少代码,用来也挺方便的。

不过最多支持5个参数,同学们可以自己加。等着下次用flexbison写一个生成工具,我感觉还是不错的。

 #pragma once
/*! \brief 动态加载dll,并调用导出函数
author:12178761@qq.com
*/
#define DLLLOADER_BEGIN( NAME)\
class DLLLOADER_##NAME\
{\
public:\
static BOOL Init( LPCWSTR sDllPath){\
if ( RefHModule())\
{\
return TRUE;\
}\
RefHModule() = ::LoadLibrary( sDllPath);\
if ( !RefHModule())\
{\
return FALSE;\
}\
return TRUE;\
}\
static void UnInit(){\
if ( RefHModule())\
{\
FreeLibrary( RefHModule());\
RefHModule() = NULL;\
}\
}\
static HMODULE& RefHModule(){\
static HMODULE M = NULL;\
return M;\
}
#define DLLLOADER_ENTRY_0( FUNNAME, FUNDESC, FUNRET, FUNRETDEF)\
typedef FUNRET (FUNDESC *_##FUNNAME)();\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static FUNRET FUNNAME(){\
if ( Ref##FUNNAME())\
{\
return Ref##FUNNAME()();\
}\
return FUNRETDEF;\
}
#define DLLLOADER_ENTRY_VOID0( FUNNAME, FUNDESC)\
typedef void (FUNDESC *_##FUNNAME)();\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static void FUNNAME(){\
if ( Ref##FUNNAME())\
{\
Ref##FUNNAME()();\
}\
}
#define DLLLOADER_ENTRY_1( FUNNAME, FUNDESC, FUNRET, FUNRETDEF, PARAMTYPE1)\
typedef FUNRET (FUNDESC *_##FUNNAME)(PARAMTYPE1);\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static FUNRET FUNNAME( PARAMTYPE1 PARAM1){\
if ( Ref##FUNNAME())\
{\
return Ref##FUNNAME()( PARAM1);\
}\
return FUNRETDEF;\
}
#define DLLLOADER_ENTRY_VOID1( FUNNAME, FUNDESC, PARAMTYPE1)\
typedef void (FUNDESC *_##FUNNAME)(PARAMTYPE1);\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static void FUNNAME( PARAMTYPE1 PARAM1){\
if ( Ref##FUNNAME())\
{\
Ref##FUNNAME()( PARAM1);\
}\
}
#define DLLLOADER_ENTRY_2( FUNNAME, FUNDESC, FUNRET, FUNRETDEF, PARAMTYPE1, PARAMTYPE2)\
typedef FUNRET (FUNDESC *_##FUNNAME)(PARAMTYPE1, PARAMTYPE2);\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static FUNRET FUNNAME( PARAMTYPE1 PARAM1, PARAMTYPE2 PARAM2){\
if ( Ref##FUNNAME())\
{\
return Ref##FUNNAME()( PARAM1, PARAM2);\
}\
return FUNRETDEF;\
}
#define DLLLOADER_ENTRY_VOID2( FUNNAME, FUNDESC, PARAMTYPE1, PARAMTYPE2)\
typedef void (FUNDESC *_##FUNNAME)(PARAMTYPE1, PARAMTYPE2);\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static void FUNNAME( PARAMTYPE1 PARAM1, PARAMTYPE2 PARAM2){\
if ( Ref##FUNNAME())\
{\
Ref##FUNNAME()( PARAM1, PARAM2);\
}\
}
#define DLLLOADER_ENTRY_3( FUNNAME, FUNDESC, FUNRET, FUNRETDEF, PARAMTYPE1, PARAMTYPE2, PARAMTYPE3)\
typedef FUNRET (FUNDESC *_##FUNNAME)(PARAMTYPE1, PARAMTYPE2, PARAMTYPE3);\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static FUNRET FUNNAME( PARAMTYPE1 PARAM1, PARAMTYPE2 PARAM2, PARAMTYPE3 PARAM3){\
if ( Ref##FUNNAME())\
{\
return Ref##FUNNAME()( PARAM1, PARAM2, PARAM3);\
}\
return FUNRETDEF;\
}
#define DLLLOADER_ENTRY_VOID3( FUNNAME, FUNDESC, PARAMTYPE1, PARAMTYPE2, PARAMTYPE3)\
typedef void (FUNDESC *_##FUNNAME)(PARAMTYPE1, PARAMTYPE2, PARAMTYPE3);\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static void FUNNAME( PARAMTYPE1 PARAM1, PARAMTYPE2 PARAM2, PARAMTYPE3 PARAM3){\
if ( Ref##FUNNAME())\
{\
Ref##FUNNAME()( PARAM1, PARAM2, PARAM3);\
}\
}
#define DLLLOADER_ENTRY_4( FUNNAME, FUNDESC, FUNRET, FUNRETDEF, PARAMTYPE1,\
PARAMTYPE2, PARAMTYPE3, PARAMTYPE4)\
typedef FUNRET (FUNDESC *_##FUNNAME)(PARAMTYPE1, PARAMTYPE2, PARAMTYPE3, PARAMTYPE4);\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static FUNRET FUNNAME( PARAMTYPE1 PARAM1, PARAMTYPE2 PARAM2, PARAMTYPE3 PARAM3, PARAMTYPE4 PARAM4){\
if ( Ref##FUNNAME())\
{\
return Ref##FUNNAME()( PARAM1, PARAM2, PARAM3, PARAM4);\
}\
return FUNRETDEF;\
}
#define DLLLOADER_ENTRY_VOID4( FUNNAME, FUNDESC, PARAMTYPE1,\
PARAMTYPE2, PARAMTYPE3, PARAMTYPE4)\
typedef void (FUNDESC *_##FUNNAME)(PARAMTYPE1, PARAMTYPE2, PARAMTYPE3, PARAMTYPE4);\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static void FUNNAME( PARAMTYPE1 PARAM1, PARAMTYPE2 PARAM2, PARAMTYPE3 PARAM3, PARAMTYPE4 PARAM4){\
if ( Ref##FUNNAME())\
{\
Ref##FUNNAME()( PARAM1, PARAM2, PARAM3, PARAM4);\
}\
}
#define DLLLOADER_ENTRY_5( FUNNAME, FUNDESC, FUNRET, FUNRETDEF, PARAMTYPE1,\
PARAMTYPE2, PARAMTYPE3, PARAMTYPE4, PARAMTYPE5)\
typedef FUNRET (FUNDESC *_##FUNNAME)(PARAMTYPE1, \
PARAMTYPE2, PARAMTYPE3, PARAMTYPE4, PARAMTYPE5);\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static FUNRET FUNNAME( PARAMTYPE1 PARAM1, PARAMTYPE2 PARAM2, \
PARAMTYPE3 PARAM3, PARAMTYPE4 PARAM4, PARAMTYPE5 PARAM5){\
if ( Ref##FUNNAME())\
{\
return Ref##FUNNAME()( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5);\
}\
return FUNRETDEF;\
}
#define DLLLOADER_ENTRY_VOID5( FUNNAME, FUNDESC, PARAMTYPE1,\
PARAMTYPE2, PARAMTYPE3, PARAMTYPE4, PARAMTYPE5)\
typedef void (FUNDESC *_##FUNNAME)(PARAMTYPE1, \
PARAMTYPE2, PARAMTYPE3, PARAMTYPE4, PARAMTYPE5);\
static _##FUNNAME Ref##FUNNAME(){\
_##FUNNAME F = (_##FUNNAME)::GetProcAddress( RefHModule(), #FUNNAME);\
return F;\
}\
static void FUNNAME( PARAMTYPE1 PARAM1, PARAMTYPE2 PARAM2, \
PARAMTYPE3 PARAM3, PARAMTYPE4 PARAM4, PARAMTYPE5 PARAM5){\
if ( Ref##FUNNAME())\
{\
Ref##FUNNAME()( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5);\
}\
}
#define DLLLOADER_END()\
}; /*
例子:
DLLLOADER_BEGIN( SAMPLE)
DLLLOADER_ENTRY_0( Function1, BOOL, FALSE, )
DLLLOADER_ENTRY_VOID0( Function2, )
DLLLOADER_ENTRY_1( Function3, BOOL, FALSE, , LPCWSTR)
DLLLOADER_END() DLLLOADER_BEGIN(KERNEL32)
DLLLOADER_ENTRY_1( LoadLibraryW, WINAPI, HMODULE, 0, LPCWSTR)
DLLLOADER_ENTRY_3( GetModuleFileNameW, WINAPI, DWORD, 0, HMODULE, LPWSTR, DWORD)
DLLLOADER_END() int _tmain(int argc, _TCHAR* argv[])
{
DLLLOADER_SAMPLE::Init( L"C:\\Users\\sosopop\\Documents\\Visual Studio 2005\\Projects\\testHandle\\debug\\testLoadDll.dll");
DLLLOADER_SAMPLE::Function1();
DLLLOADER_SAMPLE::Function2();
DLLLOADER_SAMPLE::Function3(L"Function3 Hello");
DLLLOADER_SAMPLE::Function4(L"Function4", L"Hello");
DLLLOADER_SAMPLE::UnInit(); DLLLOADER_KERNEL32::Init( L"c:\\windows\\system32\\kernel32.dll");
HMODULE h =DLLLOADER_KERNEL32::LoadLibraryW( L"user32.dll");
WCHAR sFileName[MAX_PATH]={0};
DWORD nSize = DLLLOADER_KERNEL32::GetModuleFileNameW( NULL, sFileName, _countof(sFileName)); return 0;
}
*/
上一篇:timeline自适应时间轴


下一篇:Android性能优化之利用LeakCanary检测内存泄漏及解决办法