每次加载dll时都很麻烦,然后实现了个加载dll的通用类,这样每次需要加载dll的时候只要类中继承此类并实现获取dll路径的方法就很容易加载了,但是dll的导出函数需要和此类获取的一致
以下就是实现了流程
typedef int (__stdcall * FunCreateObject)(void**, int);
typedef int (__stdcall * FunRelaseObject)(void*, int);
class CLoadDll
{
public:
CLoadDll() :
m_hDll(NULL),
m_pFunCreateObject(NULL),
m_pFunRelaseObject(NULL)
{
}
virtual CString _GetDllFilePath()
{
//每一个子类应该实现这个
return L"";
}
protected:
BOOL _Init(void)
{
if (m_hDll != NULL)
return TRUE;
if (!_LoadLibrary(_GetDllFilePath()))
return FALSE;
if (!_GetFuncAddr())
return FALSE;
return TRUE;
}
BOOL _ReleaseData(void* pData, int nID)
{
if (m_pFunRelaseObject != NULL && pData != NULL)
m_pFunRelaseObject(pData, nID);
return TRUE;
}
BOOL _LoadLibrary(const CString& strFilePath)
{
if (!PathFileExists(strFilePath))
return FALSE;
m_hDll = NULL;
m_hDll = LoadLibrary(strFilePath);
if (m_hDll == NULL)
return FALSE;
return TRUE;
}
BOOL _GetFuncAddr(void)
{
if (m_hDll == NULL)
return FALSE;
m_pFunCreateObject = (FunCreateObject)GetProcAddress(m_hDll, "CreateObject");
m_pFunRelaseObject = (FunRelaseObject)GetProcAddress(m_hDll, "ReleaseObject");
if (m_pFunCreateObject == NULL || m_pFunRelaseObject == NULL)
return FALSE;
return TRUE;
}
BOOL _GetObject(void** pData, int nID)
{
if (m_pFunCreateObject)
return m_pFunCreateObject(pData, nID);
return FALSE;
}
private:
FunCreateObject m_pFunCreateObject;
FunRelaseObject m_pFunRelaseObject;
HMODULE m_hDll;
};