Skip to main content

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.

    BEGIN_INTERFACE_PART ( MyMathObj, IMyMath )
    STDMETHOD_ ( LONG, MyAdd ) ( INT, INT ) ;
    STDMETHOD_( LONG, MySubtract ) ( INT, INT ) ;
    END_INTERFACE_PART ( MyMathObj )
    DECLARE_INTERFACE_MAP( )
    DECLARE_OLECREATE ( CMyMath )

  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 }
    IMPLEMENT_OLECREATE ( CMyMath, "MyMathDLL.MyMath",
    0xee6e8f60, 0xfecc, 0x11d2, 0xa6, 0xe6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 );
    BEGIN_INTERFACE_MAP ( CMyMath, CCmdTarget )
    INTERFACE_PART ( CMyMath, IID_IMyMath, MyMathObj )
    END_INTERFACE_MAP( )

  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.

Comments

Popular posts from this blog

MFC - Microsoft Foundation Classes Design Patterns

1 Introduction This paper describes the use of object-oriented software design patterns, as presented in Design Patterns: Elements of Reusable Object-Oriented Software by Gamma et al., within the Microsoft Foundation Class Library (MFC). MFC is used for implementing applications for Microsoft Windows operating systems. Because of the size of the MFC library, a complete analysis would have been beyond the scope of this assignment. Instead, we identified various possible locations for design patterns, using the class hierachy diagram of MFC, and studied the source carefully at these locations. When we did not find a pattern where we expected one, we have documented it anyway, with examples of how the particular problem could have been solved differently, perhaps more elegantly, using design patterns. We have included a brief introduction to MFC in Section 2 , as background information. The analysis has been split into three parts, with one section for each major design pattern ca...

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed. Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism: Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are. Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives ...

• Why might you need exception handling be used in the constructor when memory allocation is involved?

Your first reaction should be: "Never use memory allocation in the constructor." Create a separate initialization function to do the job. You cannot return from the constructor and this is the reason you may have to use exception handling mechanism to process the memory allocation errors. You should clean up whatever objects and memory allocations you have made prior to throwing the exception, but throwing an exception from constructor may be tricky, because memory has already been allocated and there is no simple way to clean up the memory within the constructor.