Real COM Example

A Real COM Example Using MFC

In this article, we would explore an in-process COM component in the form of a DLL that support IMyMath interface. We would then write a real COM client that would use this component.

In programming, there are often many ways to solve a problem. MFC and VC++ programming is not an exception to this. The way we code our component in this example is just one way to do it. We want you to learn the interaction between code that wizards provide and the code that you need to write in a simplest possible manner. We hope our approach enables you to understand a more complex programming you might encounter in other places.

Developer Studio’s AppWizard is not well optimized to generate a COM DLL. We will try to get as much support from it as possible. In the process, you will see some redundant code that we never use. But don’t pay too much attention to it at this time. As you become more accustomed to the surroundings, you may want to start optimizing things further. At this time, concentrate on the main tasks.

First we guide you through the exact process of adding the necessary code and building the DLL. You might not understand every line at this stage. But rest assured that after you go through the detailed discussion at the end, everything will make sense. Please follow these steps to create the DLL.

  1. Create a new project of type MFC Appwizard (DLL). We named it MathDLL.

    Figure 1.
    MFC AppWizard (dll).

  2. In step 1 of 1 of the AppWizard, select Automation. Though we do not need nor understand automation yet, we still need some basic COM code that Appwizard wouldn't generate otherwise. Use defaults for the rest of the choices. Click finish.

    Figure 2.

    Figure 3.

  3. Use ClassWizard to generate a new class CMyMath derived from CCmdTarget. We are going to use CMyMath to implement IMyMath interface. For automation, choose None.

  4. Create a header file named ‘interface.h’ in the project directory. Add to it the IMyMath interface declaration that is shown in the program listing. Add an include statement to include this file in ‘mymath.h’ just before CMyMath class declaration.

  5. Add the following lines to CMyMath declaration in ‘MyMath.h’. Don’t lose your sleep over this strange code. You are just beginning to see the MFC magic of interface maps and class factories. Well, we haven’t told you enough to figure that out yet.

    STDMETHOD_ ( LONG, MyAdd ) ( INT, INT ) ;
    STDMETHOD_( LONG, MySubtract ) ( INT, INT ) ;

  6. Add the following lines to ‘MyMath.cpp’ file after the IMPLEMENT_DYNCREATE macro. As you would discover later, these lines populate the interface maps and accomplish few other tasks.

    // {EE6E8F60-FECC-11d2-A6E6-000000000000 }
    0xee6e8f60, 0xfecc, 0x11d2, 0xa6, 0xe6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 );
    BEGIN_INTERFACE_MAP ( CMyMath, CCmdTarget )
    INTERFACE_PART ( CMyMath, IID_IMyMath, MyMathObj )

  7. Implement the IUnknown and IMyMath interface functions to ‘MyMath.cpp’.

  8. Hit the ‘Rebuild All’ button and the component is ready to roll. When it is fully compiled, click ‘Tools | Register Control’ button of DevStudio to register the component in the system registry.

No comments: