MFCATL: Uses ATL COM Objects in an MFC Server

Click to open or copy the MFCATL project files.

This sample illustrates how ATL COM objects can be used in an MFC server EXE.

The server allows the creation of two objects, ObjectOne (implemented in MFC and supporting a dispinterface) and ObjectTwo (implemented in ATL and supporting a dual interface).

Running the Sample

  1. Build the Mfcatl.exe server and run it standalone to register it.

  2. Load the Mfcatl.vbp Visual Basic 4.0 project, run it, and press the buttons in the form.

Note that if you are running the sample on NT 3.51, only the release build will work.

Remarks

Originally, MFCATL had both objects implemented in MFC. Both were derived from CCmdTarget. ObjectTwo was then reimplemented using ATL by following these steps:

  1. Include the ATL header files (Atlbase.h and Atlcom.h) into Premfcat.h.

  2. Include Atlimpl.cpp into Premfcat.cpp.

  3. Add a CComModule derived class to Prefcat.h (similar to the BEEPER EXE sample). The derived class implements the Lock and Unlock methods to forward lock counts to MFC by calling AfxOleLockApp and AfxOleUnlockApp).

  4. Add an object map macro pair (BEGIN_OBJECT_MAP/END_OBJECT_MAP) into the Mfcatl.cpp and add a static instance of the module class called _Module.

  5. Call _Module.Init and Term from InitInstance and ExitInstance

  6. Add the typelib as a resource

  7. Call _Module.RegisterServer(TRUE) from InitInstance to match the MFC call to COleObjectFactory::UpdateRegistryAll

  8. Call _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE) to match the REGCLS_MULTIPLEUSE of MFC's class factories, as implicitly done by the IMPLEMENT_OLECREATE macro

  9. Reimplement ObjectTwo in ATL by following these steps:
    • Change the ODL file from dispinterface to dual interface

    • Have the ODL compilation generate a header file (Interf.h) needed by ATL

    • Rewrite .h and .cpp files (the quickest way is to run ATL Wizard and copy, paste, and rename the automatically generated code in place of the original MFC code)

    • Add an OBJECT_ENTRY(CLSID_ObjectTwo, CObjectTwo) to the ATL object map

Additional conversion steps (not covered here) might include:

This sample uses the following keywords:

AfxMessageBox; AfxOleInit; AfxOleLockApp; AfxOleUnlockApp; ASSERT; BEGIN_COM_MAP; CCmdTarget; CCmdTarget::OnFinalRelease; CComCoClass; CComModule; CComModule::GetLockCount; CComModule::Lock; CComModule::Unlock; CComObjectRoot; CDialog; CMenu::AppendMenu; COleObjectFactory::RegisterAll; COleObjectFactory::UpdateRegistryAll; COM_INTERFACE_ENTRY; CString::IsEmpty; CString::LoadString; CWindow::GetSystemMenu; DECLARE_DYNCREATE; DECLARE_NOT_AGGREGATABLE; DECLARE_REGISTRY; DestroyWindow; Enable3dControls; Enable3dControlsStatic; EnableAutomation; GetClientRect; GetSystemMetrics; IDispatchImpl; PostMessage; SendMessage; SetIcon; ShowWindow; SysAllocString