/****************************************************************************** Copyright(C):2015~2018 hzleaper FileName:DllLoader.h Author:zhikun wu Email:zk.wu@hzleaper.com Tools:vs2010 pc on company Created:2015/04/27 History:27:4:2015 10:35 *******************************************************************************/ #ifndef __DLL_LOADER_H #define __DLL_LOADER_H #include "zclasses.h" #include "zfunctions.h" template class CDllLoader { public: typedef initOut (*Func_Lib_Init)(initIn); typedef freeOut (*Func_Lib_Free)(freeIn); public: CDllLoader(const char *szLibName, const char *szFuncInit, const char *szFuncFree, const char* szPath = NULL) : m_lib(szLibName) { if( NULL != szPath ) { m_lib.setFileName(ZString(szPath) + szLibName); } m_fpInit = (Func_Lib_Init)m_lib.resolve(szFuncInit); m_fpFree = (Func_Lib_Free)m_lib.resolve(szFuncFree); } CDllLoader(const char *szLibName, const char *szFuncInit, const char *szFuncFree, const ZStringList& dllPaths) { ZString szName(szLibName); #ifdef _DEBUG szName.append("d"); #endif ZString szPath; for (int i = 0; i < dllPaths.size(); ++i) { szPath = dllPaths[i]; if (ZKF::qtDllExit(szPath, szName)) { break; } } // m_lib.setFileName(szPath + szName); m_fpInit = (Func_Lib_Init)m_lib.resolve(szFuncInit); m_fpFree = (Func_Lib_Free)m_lib.resolve(szFuncFree); } ~CDllLoader(void) { m_lib.unload(); } initOut ModuleInit(initIn inParam) { if( NULL == m_fpInit ) { tpDebugOut("Failed to get the export init function in module init"); return NULL; } return m_fpInit(inParam); } freeOut ModuleFree(freeIn in) { if( NULL != m_fpFree ) { m_fpFree(in); // m_fpFree = NULL; } } private: ZLibrary m_lib; Func_Lib_Init m_fpInit; Func_Lib_Free m_fpFree; }; ///////////////////////////////////////////////////////////////////////////////////////////////////////// class CDllLoaderM { public: typedef void*(*Func_Lib_Init)(void*); typedef void(*Func_Lib_Free)(); public: CDllLoaderM(const char *szLibName, const char *szFuncInit, const char *szFuncFree, const char* szPath = NULL) : m_lib(szLibName) { if (NULL != szPath) { m_lib.setFileName(ZString(szPath) + szLibName); } //tpDebugOut("single paths: %s, name:%s", szPath, szLibName); m_fpInit = (Func_Lib_Init)m_lib.resolve(szFuncInit); m_fpFree = (Func_Lib_Free)m_lib.resolve(szFuncFree); //tpDebugOut("init function, Name: %s, Address:%d", szFuncInit, (int)m_fpInit); } CDllLoaderM(const char *szLibName, const char *szFuncInit, const char *szFuncFree, const ZStringList& dllPaths) { ZString szName(szLibName); #ifdef _DEBUG szName.append("d"); #endif ZString szPath; for (int i = 0; i < dllPaths.size(); ++i){ szPath = dllPaths[i]; if (ZKF::qtDllExit(szPath, szName)){ break; } } //tpDebugOut("Multi paths: %s, name:%s", szPath.toLocal8Bit().data(), szName.toLocal8Bit().data()); // m_lib.setFileName(szPath + szName); if (m_lib.load()) { m_fpInit = (Func_Lib_Init)m_lib.resolve(szFuncInit); m_fpFree = (Func_Lib_Free)m_lib.resolve(szFuncFree); //tpDebugOut("init function, Name: %s, Address:%d", szFuncInit, (int)m_fpInit); } else { m_fpInit = NULL; m_fpFree = NULL; tpDebugOut("DllLoader Error: %s", m_lib.errorString().toLocal8Bit().data()); } } ~CDllLoaderM(void) { m_lib.unload(); } void* ModuleInit(void* inParam) { if (NULL == m_fpInit) { tpDebugOut("fail to get Init Function in dll:%s", m_lib.fileName().toLocal8Bit().data()); return NULL; } return m_fpInit(inParam); } void ModuleFree() { if (NULL != m_fpFree) { m_fpFree(); // m_fpFree = NULL; } } private: ZLibrary m_lib; Func_Lib_Init m_fpInit; Func_Lib_Free m_fpFree; }; #endif