MFCATL Sample: Uses ATL COM Objects in an MFC Server

The MFCATL 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).

Security noteSecurity Note

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

To access samples from Visual Studio

  • On the Help menu, click Samples.

    By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.

  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

Building and Running the Sample

To build and run the sample

  1. Open the solution file mfcatl.sln.

  2. From the Build menu, click Build Solution.

  3. From the Debug menu, click Start Without Debugging. This will run the mfcatl.exe server standalone and will register it.

  4. Open mfcatl.htm file in your Web browser and click the buttons to call into appropriate objects. You can call each object individually or both at the same time.

Conversion Remarks

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

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

  2. Include Atlimpl.cpp in Premfcat.cpp.

  3. Add a CAtlModule-derived class to Prefcat.h. 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) in 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 the ATL wizards 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:

  • Porting the ODL file to IDL format.

  • Adding support for -RegServer and -UnregServer command-line arguments.

Keywords

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; EnableAutomation; GetClientRect; GetSystemMetrics; IDispatchImpl; PostMessage; SendMessage; SetIcon; ShowWindow; SysAllocString

See Also

Other Resources

ATL Samples