Click here to Skip to main content
13,095,218 members (87,164 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 17 Mar 2005

ATL COM EXE Server with MFC support

, 17 Mar 2005
Rate this:
Please Sign up or sign in to vote.
Adding MFC support to the ATL COM EXE Server.

Sample image


This article will explain implementing an ATL COM EXE server with MFC support. As an example, we create an ATL COM EXE server that can create an MFC child window and an ATL child window in the client side’s parent window.


In a recent project, we are trying to add MFC Support to an ATL EXE server. We want to create both MFC window using CWnd derived class and ATL window using CWindow in our server. There was an article in MSDN about this :'Q 173974 : HOWTO: Add MFC Support to an ATL Project'. And this is the main reference from which we created our example application.

Using the code

The code consists of two Microsoft Visual C++ 6.0 projects:

  • ATLMFCCOMClient - Client side implementation. This is a simple SDI application.
  • ATLMFCCOMServer - Server side implementation.

These projects support both non-Unicode and Unicode compilation.

Server side implementation

This project was first created using ATL COM Wizard, and then modified to support MFC according MSDN's article Q 173974. However, this is not enough; there are other three modifications we should add:

  1. Add AtlAxWinInit() in CMyApp::InitInstance().

    This will allow us create ATL window correctly. See Figure 1.

  2. Add _Module.StartMonitor() in CMyApp::InitInstance().

    2 and 3 allow the EXE server exit correctly when the client exits. See Figure 1.

  3. Add theApp.PostThreadMessage( WM_QUIT, 0, 0) in CExeModule::MonitorShutdown().

    See Figure 2.

BOOL CMyApp::InitInstance()
        // Initialize OLE libraries
        if (!AfxOleInit())
           AfxMessageBox(_T("OLE Initialization Failed!"));
           return FALSE;

        // Initialize the ATL Module
     #ifdef _AFXDLL
        Enable3dControls(); // Call this when using MFC in a shared DLL
        Enable3dControlsStatic(); // Call this when linking
                                  // to MFC statically
        // 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
        // ATL ones specifically register with 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.

Figure 1

//Monitors the shutdown event
void CExeModule::MonitorShutdown()
    while (1)
        WaitForSingleObject(hEventShutdown, INFINITE);
        DWORD dwWait=0;
            bActivity = false;
            dwWait = WaitForSingleObject(hEventShutdown, dwTimeOut);
        } while (dwWait == WAIT_OBJECT_0);
        // timed out
        if (!bActivity && m_nLockCnt == 0) // if no activity let's really bail
#if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
            if (!bActivity && m_nLockCnt == 0)
    PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
    theApp.PostThreadMessage( WM_QUIT, 0, 0);


Figure 2

We created two COM objects: ATLWindowObject and MFCWindowObject.

ATLWindowObject has two interfaces: CreateATLWindow and Update.

MFCWindowObject has two interfaces: CreateMFCWindow and Update.

Both Update() interfaces are used to refresh the window when the client side parent window needs to be refreshed. See Client side implementation section.

We also created two window classes :CMFCWnd derived from CWnd and CATLWnd derived from CWindow. CMFCWnd will be created by MFCWindowObject while CATLWnd will be created by ATLWindowObject.

CMFCWnd is easy, but CATLWnd needs adding a message map manually, See Figure 3.

class CATLWnd : public CWindowImpl<CATLWND, CWindow>
    DECLARE_WND_CLASS(_T("ATL Window Class"))
    CATLWnd() { m_hbrBkgnd = CreateSolidBrush(RGB(0,0,255)); }
    ~CATLWnd() { DeleteObject ( m_hbrBkgnd ); }

    LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
        RECT rcClient;
        PAINTSTRUCT ps;
        HDC  dc = GetDC();
        GetClientRect ( &rcClient );
        FillRect ( dc, &rcClient, m_hbrBkgnd );
        TextOut(dc,2,10,_T("ATL Window"),10);
        return 0;    
    HBRUSH m_hbrBkgnd;

Figure 3

Client side implementation

The function OnMfcWindow will be called, when user select “Create MFC Window” menu. MFCWindowObject will first be instantiated, and its interface CreateMFCWindow will be called to create a MFC window.

Likely, the function OnATLWindow will be called, when user select “Create ATLWindow” menu. ATLWindowObject will first be instantiated, and its interface CreateATLWindow will be called to create an ATL window. See Figure 4.

Note that both windows are the child windows of CATLMFCCOMClientView

void CATLMFCCOMClientView::OnMfcWindow() 
    if(m_pMFCWindowObject) return;
    HRESULT hResult = S_OK;          
    hResult = CoCreateInstance(CLSID_MFCWindowObject,

void CATLMFCCOMClientView::OnAtlWindow() 
    if(m_pATLWindowObject) return;
    HRESULT hResult = S_OK;          
    hResult = CoCreateInstance(CLSID_ATLWindowObject,

Figure 4

In server side implementation section, we mentioned that both the COM objects: ATLWindowObject and MFCWindowObject provided interface Update. This interface is used in CATLMFCCOMClientView::OnDraw to avoid window refresh issue, See Figure 5.

void CATLMFCCOMClientView::OnDraw(CDC* pDC)
    CATLMFCCOMClientDoc* pDoc = GetDocument();

Figure 5


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Huang Wei
Web Developer
China China
He got Master degree of Theory and Application of Automatic Control from Tsinghua university(Beijing,China) in 1997,from then on he has been working for Tsinghua TongFang Software Co.Ltd,Beijing China.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 1170985024-May-15 16:04
memberMember 1170985024-May-15 16:04 
General正需要哦,谢谢! Pin
mcc2vc6-Dec-07 22:30
membermcc2vc6-Dec-07 22:30 
GeneralI have an ATL COM dll and an MFC application. I need ... Pin
ehaerim27-Aug-07 9:39
memberehaerim27-Aug-07 9:39 
to merge the MFC application into the ATL COM dll project.

I tried hard but still having problems.

Mr. Huang Wei, please contact me via email or MSN messenger below:


GeneralATL/DCOM/MFC/OLE Automation Pin
info4manu27-Jul-05 1:52
memberinfo4manu27-Jul-05 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 17 Mar 2005
Article Copyright 2005 by Huang Wei
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid