HOWTO: Add MFC Support to an ATL Project

Last reviewed: March 11, 1998
Article ID: Q173974
The information in this article applies to:
  • The Microsoft Active Template Library (ATL), versions 2.0, 2.1 included with: - Microsoft Visual C++, 32-bit Editions, version 5.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 INFORMATION

Adding MFC Support to an ATL EXE Project

  1. 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
    
    

  2. 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.

  3. 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;
    
    

  4. 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 the ATL Module
            _Module.Init(ObjectMap,m_hInstance);
         #ifdef _AFXDLL
            Enable3dControls(); // Call this when using MFC in a shared DLL
         #else
            Enable3dControlsStatic(); // Call this when linking
                                      // to MFC statically
         #endif
    
            // Update the System Registry
            COleObjectFactory::UpdateRegistryAll(); // MFC Classes
            VERIFY(SUCCEEDED(_Module.RegisterServer(TRUE))); // ATL Classes
    
            // Create the dialog box or other stuff here
    
            // Register OLE Class Factories
            // MFC ones are for multiple as specified
            // by the IMPLEMENT_OLECREATE() macro
            COleObjectFactory::RegisterAll();
            // ATL ones specifically register with REGCLS_MULTIPLEUSE
            VERIFY(SUCCEEDED(_Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
                                                         REGCLS_MULTIPLEUSE)));
    
            // Parse the command line to see if launched as OLE server
            if (RunEmbedded() || RunAutomated())
            {
               // Application was run with /Embedding or /Automation.
               // Don't show the main window in this case.
               return TRUE;
            }
    
            return FALSE; // Nothing to do, so exit.
         }
    
         int CMyApp::ExitInstance()
         {
            // MFC's class factories registration is
            // automatically revoked by MFC itself
            _Module.RevokeClassObjects(); // Revoke class factories for ATL
            _Module.Term();               // cleanup ATL Global Module
            return CWinApp::ExitInstance();
      }
    
    

  5. 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:

    ARTICLE-ID: Q125750

       TITLE     : PRB: Error LNK2001: '_WinMain@16': Unresolved External
                   Symbol
    
    

  6. 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.

  1. 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()
          {
             _Module.Term();
             return CWinApp::ExitInstance();
          }
    
    

  2. 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:

    ARTICLE-ID: Q140850

       TITLE     : 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

ARTICLE-ID: Q166480

TITLE     : INFO: Active Template Library(ATL) Frequently Asked Questions

(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Chuck Bell, Microsoft Corporation
Keywords          : AtlIss
Technology        : kbMfc
Version           : WINDOWS:2.0,2.1
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 11, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.