2、修改工程设置 点击【Project Settings】对话框的【General】页, 改变 "Microsoft Foundation Classes" 下拉框的值为"Use MFC in a Static Library"或者"Use MFC in a Shared Library"
3、删除ATL工程的DLL入口部分代码 / // DLL Entry Point extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/) { if (dwReason == DLL_PROCESS_ATTACH) { _Module.Init(ObjectMap, hInstance, &LIBID_ATLTESTLib); DisableThreadLibraryCalls(hInstance); } else if (dwReason == DLL_PROCESS_DETACH) _Module.Term(); return TRUE; // ok }
4、增加基于CWinApp的视图类,在删掉位DLL入口的位置上增加下面的代码(将设你的工程类是"CATLTest ") class CATLTestApp : public CWinApp { public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CATLTestApp ) public: virtual BOOL InitInstance(); virtual int ExitInstance(); //}}AFX_VIRTUAL
//{{AFX_MSG(CATLTestApp ) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() };
BEGIN_MESSAGE_MAP(CATLTestApp , CWinApp) //{{AFX_MSG_MAP(CATLTestApp ) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP END_MESSAGE_MAP()
CATLTestApp theApp; BOOL CATLTestApp ::InitInstance() { _Module.Init(ObjectMap, m_hInstance, &LIBID_ATLTESTLib); //LIBID_ATLTESTLib should be change to your project lib return CWinApp::InitInstance(); }
int CATLTestApp ::ExitInstance() { _Module.Term(); return CWinApp::ExitInstance(); }
5、 修改DllCanUnloadNow函数为下面的代码 / // Used to determine whether the DLL can be unloaded by OLE STDAPI DllCanUnloadNow(void) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE; } 完成上面的几步操作你的ATL工程就可以很好的支持MFC。