HOWTO: Add MFC Support to an ATL Project
ID: Q173974
|
The information in this article applies to:
-
The Microsoft Active Template Library (ATL), versions 2.0, 2.1, 3.0, used with:
-
Microsoft Visual C++, 32-bit Editions, versions 5.0, 6.0
SUMMARY
When creating an ATL EXE project using the AppWizard, the MFC support check
box is disabled. This article explains how to add MFC support to an ATL EXE
project. This article also explains how to add MFC support to an ATL DLL
project if you failed to initially select the "support MFC" check box in
the AppWizard.
MORE INFORMATIONAdding MFC Support to an ATL EXE Project
- Add the following #include directives to StdAfx.h prior to including Atlbase.h:
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation extensions
- Change project settings to use MFC. From the Project Settings dialog
box, click the General tab, and change the setting in the "Microsoft
Foundation Classes" list box to MFC.
- Add a CWinApp derived class and declare a global variable of that type
as follows:
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
};
CMyApp theApp;
- Replace the _tWinMain function with the following InitInstance and
ExitInstance code:
BOOL CMyApp::InitInstance()
{
// Initialize OLE libraries.
if (!AfxOleInit())
{
AfxMessageBox(_T("OLE Initialization Failed!"));
return FALSE;
}
// Initialize CcomModule.
_Module.Init(ObjectMap,m_hInstance);
_Module.dwThreadID = GetCurrentThreadId();
// Check command line arguments.
TCHAR szTokens[] = _T("-/");
M_bRun = TRUE;
LPCTSTR lpszToken = FindOneOf(m_lpCmdLine, szTokens);
while (lpszToken != NULL)
{
// Register ATL and MFC class factories.
if (lstrcmpi(lpszToken, _T("Embedding"))==0 ||
lstrcmpi(lpszToken, _T("Automation"))==0)
{
AfxOleSetUserCtrl(FALSE);
break;
}
// Unregister servers.
// There is no unregistration code for MFC
// servers. Refer to <LINK TYPE="ARTICLE" VALUE="Q186212">Q186212</LINK> "HOWTO: Unregister MFC
// Automation Servers" for adding unregistration
// code.
else if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
{
VERIFY(SUCCEEDED(_Module.UpdateRegistryFromResource(IDR_Atlexe,
FALSE)));
VERIFY(SUCCEEDED(_Module.UnregisterServer(TRUE)));
m_bRun(4) = FALSE;
break;
}
// Register ATL and MFC objects in the registry.
else if (lstrcmpi(lpszToken, _T("RegServer"))==0)
{
VERIFY(SUCCEEDED(_Module.UpdateRegistryFromResource(IDR_Atlexe,
TRUE)));
VERIFY(SUCCEEDED(_Module.RegisterServer(TRUE)));
COleObjectFactory::UpdateRegistryAll();
m_bRun(4) = FALSE;
break;
}
lpszToken = FindOneOf(lpszToken, szTokens);
}
if (m_bRun(4))
{
// Comment out the next line if not using VC 6-generated
// code.
_Module.StartMonitor();
VERIFY(SUCCEEDED(_Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE)));
VERIFY(COleObjectFactory::RegisterAll());
// To run the EXE standalone, you need to create a window
// and assign the CWnd* to m_pMainWnd.
}
return TRUE;
}
int CMyApp::ExitInstance()
{
// MFC's class factories registration is
// automatically revoked by MFC itself.
if (m_bRun)
_Module.RevokeClassObjects();
- For Unicode builds, make sure the entry point is set to
wWinMainCRTStartup in the Output category of the Link field in the
Project Settings dialog box. For additional information, please see the following
article in the Microsoft Knowledge Base:
Q125750 PRB: Error LNK2001: '_WinMain@16': Unresolved External Symbol
- Add the following line of code to the beginning of every member function
of a COM interface, window procedure, and exported function:
AFX_MANAGE_STATE(AfxGetAppModuleState());
For more information on AFX_MANAGE_STATE, consult the VC++ online
documentation.
Adding MFC Support to an ATL DLL Project
Follow steps 1 to 3 from above.
- Move the code in the AppWizard-generated DllMain's DLL_PROCESS_ATTACH
and DLL_PROCESS_DETACH to the CMyApp's InitInstance and ExitInstance and
remove the DllMain as follows:
BOOL CMyApp::InitInstance()
{
_Module.Init(ObjectMap, m_hInstance);
return CWinApp::InitInstance();
}
int CMyApp::ExitInstance()
{
// MFC's class factories registration is
// automatically revoked by MFC itself.
if (m_bRun)
_Module.RevokeClassObjects();
- Add the following line of code to the beginning of every member function
of a COM interface, window procedure and exported function:
AFX_MANAGE_STATE(AfxGetStaticModuleState());
For additional information, please see the following
article in the Microsoft Knowledge Base:
Q140850 HOWTO: Converting DLLTRACE to Use MFC in Shared Library
NOTE: For all release builds, make sure that the _ATL_MIN_CRT preprocessor
definition has been removed. You can find these definitions in the
Preprocessor category of the C/C++ tab in the Project Settings dialog
box.
When adding a class derived from a MFC class using the ClassWizard to an
ATL EXE project or to an ATL DLL project without "MFC Support," the
compiler will issue a C2504 error.
REFERENCES
MfcAtl Sample included with Visual C++ 5.0.
For additional information, please see the following
article in the Microsoft Knowledge Base:
Q186212 HOWTO: Unregister MFC Automation Servers
(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Chuck Bell, Microsoft Corporation.
Additional query words:
checkbox listbox
Keywords : kbATL200 kbATL210 kbMFC kbVC500 kbVC600 kbATL300 kbfaq kbGrpMFCATL
Version : WINDOWS:2.0,2.1,3.0
Platform : WINDOWS
Issue type : kbhowto
|