在建立atl com接口DLL时,如果加入了Mfc支持,系统就会自动建立DLLMain函数,而且函数的位置特殊,可参见:http://www.cnblogs.com/helloboyang/p/5237648.html,
如果程序需要在DLLMain()函数中实现处理过程怎么办?如果在该项目下强行加入DLLMian会报:dllmain已经在 中定义 错误。
受上面文章启发,可利用该CWinApp 派生类的 InitInstance 成员函数实现目的 。如:
这是我的一个项目内,dllmain.cpp内容,我在其中修改了InitInstance()函数,增加了获取当前路径,已实现载入配置文件的目的。
项目名称为:ATLcomMfc,建立项目后自动生成dllmain.cpp文件,在该文件内生成了CATLcomMfcApp类,该类派生于CWinApp
class CATLcomMfcApp : public CWinApp
{
public:
// 重写
virtual BOOL InitInstance();
virtual int ExitInstance();
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CATLcomMfcApp, CWinApp)
END_MESSAGE_MAP()
CATLcomMfcApp theApp;
BOOL CATLcomMfcApp::InitInstance()
{
{//获取dll文件路径,相当于在DLLMain()函数中添加,m_hInstance为当前dll句柄
TCHAR szPathname[_MAX_PATH];
GetModuleFileName(m_hInstance, szPathname, _countof(szPathname));
PathRenameExtension(szPathname, _T(".config"));
ATLTRACE("/n<<<<<DLL配置文件路径:%S/n", szPathname);
}
#ifdef _MERGE_PROXYSTUB
if (!PrxDllMain(m_hInstance, DLL_PROCESS_ATTACH, NULL))
return FALSE;
#endif
return CWinApp::InitInstance();
}
int CATLcomMfcApp::ExitInstance()
{
return CWinApp::ExitInstance();
}
后期又看了这篇文章:http://www.cnblogs.com/profession/p/5849440.html
没有测试,可供参考。