在开发过程中,经常会引用外部函数。方法主要有两种:
方法一:包含头文件并制定lib位置
- 优点:使用简单
- 缺点:lib和vs版本有关,不同的版本和编译模式可能导致编译失败
方法二:GetProcAddress
- 优点:和编译器无关,只要获取函数地址即可使用
- 缺点:需要一个个的函数去获取地址,相对麻烦
思考:有没有好的导出方式,可以直接包含头文件就使用呢?
- 答案:有,参考Miniblink。在使用Miniblink的时候我们只需要包含头文件就可以直接引用导出函数。
源码分析:
- 作为dll的开发者,需要导出函数。模式如下:
#if defined(__cplusplus)
#define XYCENTER_EXTERN_C extern "C"
#else
#define XYCENTER_EXTERN_C
#endif
#这里定义宏,根据函数参数个数不同,定义如下函数,一直延续到NXYCENTER_DEFINE_ITERATORN
#define XYCENTER_DECLARE_ITERATOR0(returnVal, name, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name();
#define XYCENTER_DECLARE_ITERATOR1(returnVal, name, p1, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1);
#define XYCENTER_DECLARE_ITERATOR2(returnVal, name, p1, p2, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1, p2);
#格式按照【返回类型】【函数名】【参数】来维护导出函数列表0-N
#define XYCENTER_FOR_EACH_DEFINE_FUNCTION(ITERATOR0, ITERATOR1,ITERATOR2) \
ITERATOR0(void, xyShutdown, "") \
ITERATOR1(void, set_xycenterlog_handle,char *,"") \
#导出所有的函数0-N
XYCENTER_FOR_EACH_DEFINE_FUNCTION(XYCENTER_DECLARE_ITERATOR0, XYCENTER_DECLARE_ITERATOR1)
实现对应的导出函数即可:
void xyShutdown(){}
void set_xycenterlog_handle(char *){}
- 作为dll的符号使用者,分为两个步骤定义函数指针和对象,获取对应的符号地址
#定义不同参数个数的函数指针以及对应的对象0-N
#selectany 关键字,相关含义可以自行搜索
#define XYCENTER_DEFINE_ITERATOR0(returnVal, name, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR1(returnVal, name, p1, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR2(returnVal, name, p1,p2, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1,p2); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#获取函数地址
#define XYCENTER_GET_PTR_ITERATOR(name) \
name = (xyFN_##name)GetProcAddress(hMod, #name); \
if (!name) \
MessageBoxA(((HWND)0), #name##" api not found", #name, 0);
#为了兼容上述维护的函数列表,所以需要多对应获取0-N的参数模式,这里直接使用变参模式
#define XYCENTER_GET_PTR_ITERATORN(returnVal, name,...) \
XYCENTER_GET_PTR_ITERATOR(name);
#最后一步设置dll路径并且初始化
#for each 不同参数的函数地址
XYCENTER_FOR_EACH_DEFINE_FUNCTION(XYCENTER_DEFINE_ITERATOR0, XYCENTER_DEFINE_ITERATOR1, XYCENTER_DEFINE_ITERATOR2)
__declspec(selectany) const wchar_t* s_xyCenterDllPath = L"xyCenter.dll";
inline void xyCenterSetWkeDllPath(const wchar_t* dllPath)
{
s_xyCenterDllPath = dllPath;
}
inline int xyCenterInitialize()
{
HMODULE hMod = NULL;
if (!hMod)
hMod = LoadLibraryW(s_xyCenterDllPath);
if (hMod) {
XYCENTER_FOR_EACH_DEFINE_FUNCTION(XYCENTER_GET_PTR_ITERATORN, XYCENTER_GET_PTR_ITERATORN, XYCENTER_GET_PTR_ITERATORN);
return 1;
}
return 0;
}
完整头文件,目前默认支持最多10个参数,用户可以自己扩展
使用宏XYCENTER_EXPORTS
来决定是导入函数还是导出函数
#ifndef XYCENTER_DEFINE_H
#define XYCENTER_DEFINE_H
#include <windows.h>
//
#define XYCENTER_CALL_TYPE __cdecl
#if defined(__cplusplus)
#define XYCENTER_EXTERN_C extern "C"
#else
#define XYCENTER_EXTERN_C
#endif
namespace xyCenter
{
/定义函数指针以及变量/
#define XYCENTER_DEFINE_ITERATOR0(returnVal, name, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR1(returnVal, name, p1, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR2(returnVal, name, p1,p2, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1,p2); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR3(returnVal, name, p1,p2,p3, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1,p2,p3); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR4(returnVal, name, p1,p2,p3,p4, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1,p2,p3,p4); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR5(returnVal, name, p1,p2,p3,p4,p5, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1,p2,p3,p4,p5); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR6(returnVal, name, p1,p2,p3,p4,p5,p6, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1,p2,p3,p4,p5,p6); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR7(returnVal, name, p1,p2,p3,p4,p5,p6,p7, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1,p2,p3,p4,p5,p6,p7); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR8(returnVal, name, p1,p2,p3,p4,p5,p6,p7,p8, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1,p2,p3,p4,p5,p6,p7,p8); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR9(returnVal, name, p1,p2,p3,p4,p5,p6,p7,p8,p9, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1,p2,p3,p4,p5,p6,p7,p8,p9); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
#define XYCENTER_DEFINE_ITERATOR10(returnVal, name, p1,p2,p3,p4,p5,p6,p7,p8,p9,p10, description) \
typedef returnVal(XYCENTER_CALL_TYPE* xyFN_##name)(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10); \
__declspec(selectany) xyFN_##name name = ((xyFN_##name)0);
/获取dll函数指针地址/
#define XYCENTER_GET_PTR_ITERATOR(name) \
name = (xyFN_##name)GetProcAddress(hMod, #name); \
if (!name) \
MessageBoxA(((HWND)0), #name##" api not found", #name, 0);
#define XYCENTER_GET_PTR_ITERATORN(returnVal, name,...) \
XYCENTER_GET_PTR_ITERATOR(name);
/定义导出函数/
#define XYCENTER_DECLARE_ITERATOR0(returnVal, name, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name();
#define XYCENTER_DECLARE_ITERATOR1(returnVal, name, p1, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1);
#define XYCENTER_DECLARE_ITERATOR2(returnVal, name, p1, p2, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1, p2);
#define XYCENTER_DECLARE_ITERATOR3(returnVal, name, p1, p2,p3, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1, p2,p3);
#define XYCENTER_DECLARE_ITERATOR4(returnVal, name, p1, p2,p3,p4, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1, p2,p3,p4);
#define XYCENTER_DECLARE_ITERATOR5(returnVal, name, p1, p2,p3,p4,p5, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1, p2,p3,p4,p5);
#define XYCENTER_DECLARE_ITERATOR6(returnVal, name, p1, p2,p3,p4,p5,p6, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1, p2,p3,p4,p5,p6);
#define XYCENTER_DECLARE_ITERATOR7(returnVal, name, p1, p2,p3,p4,p5,p6,p7, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1, p2,p3,p4,p5,p6,p7);
#define XYCENTER_DECLARE_ITERATOR8(returnVal, name, p1, p2,p3,p4,p5,p6,p7,p8, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1, p2,p3,p4,p5,p6,p7,p8);
#define XYCENTER_DECLARE_ITERATOR9(returnVal, name, p1, p2,p3,p4,p5,p6,p7,p8,p9, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1, p2,p3,p4,p5,p6,p7,p8,p9);
#define XYCENTER_DECLARE_ITERATOR10(returnVal, name, p1, p2,p3,p4,p5,p6,p7,p8,p9,p10, description) \
XYCENTER_EXTERN_C __declspec(dllexport) returnVal name(p1, p2,p3,p4,p5,p6,p7,p8,p9,p10);
// 以下是xyCenter的导出函数。格式按照【返回类型】【函数名】【参数】来排列
#define XYCENTER_FOR_EACH_DEFINE_FUNCTION(ITERATOR0, ITERATOR1,ITERATOR2,ITERATOR3,ITERATOR4,ITERATOR5,ITERATOR6,ITERATOR7,ITERATOR8,ITERATOR9,ITERATOR10) \
ITERATOR0(int, get_msgno, "获取通知消息的消息code") \
ITERATOR1(void, set_log_handle, PFN_XYCENTERLOG_HANDLE,"设置日志句柄") \
ITERATOR1(bool, write_data, XyCenterData *,"写入用户共享数据") \
ITERATOR1(bool, read_data, XyCenterData&,"获取用户数据") \
//导出函数
#ifdef XYCENTER_EXPORTS
XYCENTER_FOR_EACH_DEFINE_FUNCTION(XYCENTER_DECLARE_ITERATOR0, XYCENTER_DECLARE_ITERATOR1, XYCENTER_DECLARE_ITERATOR2,\
XYCENTER_DECLARE_ITERATOR3,XYCENTER_DECLARE_ITERATOR4, XYCENTER_DECLARE_ITERATOR5, XYCENTER_DECLARE_ITERATOR6,\
XYCENTER_DECLARE_ITERATOR7, XYCENTER_DECLARE_ITERATOR8,XYCENTER_DECLARE_ITERATOR9, XYCENTER_DECLARE_ITERATOR10)
//引入导出函数
#else
XYCENTER_FOR_EACH_DEFINE_FUNCTION(XYCENTER_DEFINE_ITERATOR0, XYCENTER_DEFINE_ITERATOR1, XYCENTER_DEFINE_ITERATOR2,\
XYCENTER_DEFINE_ITERATOR3, XYCENTER_DEFINE_ITERATOR4, XYCENTER_DEFINE_ITERATOR5, XYCENTER_DEFINE_ITERATOR6,\
XYCENTER_DEFINE_ITERATOR7, XYCENTER_DEFINE_ITERATOR8, XYCENTER_DEFINE_ITERATOR9, XYCENTER_DEFINE_ITERATOR10)
__declspec(selectany) const wchar_t* s_xyCenterDllPath = L"xyCenter.dll";
inline void SetDllPath(const wchar_t* dllPath)
{
s_xyCenterDllPath = dllPath;
}
inline bool Initialize()
{
HMODULE hMod = NULL;
if (!hMod)
hMod = LoadLibraryW(s_xyCenterDllPath);
if (hMod) {
XYCENTER_FOR_EACH_DEFINE_FUNCTION(XYCENTER_GET_PTR_ITERATORN, XYCENTER_GET_PTR_ITERATORN, XYCENTER_GET_PTR_ITERATORN,\
XYCENTER_GET_PTR_ITERATORN, XYCENTER_GET_PTR_ITERATORN, XYCENTER_GET_PTR_ITERATORN, XYCENTER_GET_PTR_ITERATORN,\
XYCENTER_GET_PTR_ITERATORN, XYCENTER_GET_PTR_ITERATORN, XYCENTER_GET_PTR_ITERATORN, XYCENTER_GET_PTR_ITERATORN);
return true;
}
return false;
}
#endif // DEBUG
};
#endif // XYCENTER_DEFINE_H
此时我们只需要包含头文件,并且初始化一下即可直接使用导出函数。