1. 在头文件中如下定义一些在三个系统中具有相同功能但是不同名称的函数和文件类型:
#if defined(_WINDOWS_)
#include <windows.h>
#include <stdio.h>
#include <wtypes.h>
#define LOAD_OBJECT(x) LoadLibraryA(x)
#define UNLOAD_OBJECT FreeLibrary
#define LOAD_PROC GetProcAddress
#define OBJECT_TYPE HMODULE
#define BASESDK_OBJECT_NAME "*******.dll"
#define LOAD_FUNCTION_PTR( funcPtr, funcType, funcName , handle ) /
if( ! funcPtr ) { /
funcPtr = (funcType)GetProcAddress( handle, funcName ); /
if( ! funcPtr ) { /
return 1; /
} /
}
#elif DEFINED_MAC
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <dlfcn.h>
#include <time.h>
#define LOAD_OBJECT(x) dlopen( x, RTLD_LAZY )
#define UNLOAD_OBJECT dlclose
#define LOAD_PROC dlsym
#define OBJECT_TYPE void*
#define BASESDK_OBJECT_NAME "********"
#ifndef MAX_PATH
#define MAX_PATH 255
#endif
#define LOAD_FUNCTION_PTR( funcPtr, funcType, funcName , handle ) /
if( ! funcPtr ) { /
funcPtr = (funcType)dlsym( handle, funcName ); /
if( ! funcPtr ) { /
return -1; /
} /
}
#else
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <dlfcn.h>
#include <time.h>
#define LOAD_OBJECT(x) dlopen( x, RTLD_LAZY)
#define UNLOAD_OBJECT dlclose
#define LOAD_PROC dlsym
#define OBJECT_TYPE void*
#define BASESDK_OBJECT_NAME "******.so"
#define LOAD_FUNCTION_PTR( funcPtr, funcType, funcName , handle ) /
if( ! funcPtr ) { /
funcPtr = (funcType)dlsym( handle, funcName ); /
if( ! funcPtr ) { /
return -1; /
} /
}
#endif
2. 在实现文件中,通过重新定义的函数来调用实现,示例如下:
Sdk****::Sdk*****()
{
m_session = 0;
m_hDll = 0;
m_hDll = LOAD_OBJECT( BASESDK_OBJECT_NAME );
}
采用以上这种方式就可以避免不同系统中因为函数名称不同而引起的问题。