COM: Creating a COM Component using C++ (Part-1)

COM (Component Object Model) is a platform-independent, distributed and object-oriented technology developed by Microsoft to create re-usable software components and enables software components to communicate.

COM enables interaction between objects through interfaces. Interfaces are core concepts to build COM components. An interface contains function prototypes not the function implementations. COM interfaces are strongly typed and every interface has its own unique interface identifier (called GUID – Globally Unique IDentifier). Component developers must provide interface implementation and associate the implementation with interfaces.

COM Component expose the interfaces to COM Clients (those are going to use COM Components) or another COM Components. COM Clients will call the methods in interfaces and because of the association between interfaces and interface implementations, methods defined in COM Components will execute.

COM components can be created with variety of programming languages. In this article we are going to create a simple COM component using C++ programming language. As this concept is difficult to understand, we are going to explain this in series of articles; instead of a single article.

Before going to start implementing a COM component, lets have a look at basic interface IUnknown.

IUnknown interface

This is the core interface and must be implemented as part of every interface. IUnknown interface has 3 methods, two of them (AddRef and Release methods) are used for reference counting and QueryInterface method is used to retrieve pointer to object’s interfaces. Reference count controls the life of the object. When no references to the object, object should be destroyed.

Now the time for implementation.

Step (1). The first step is define an interface using Interface Definition Language (IDL) and save it in .idl file. Each interface in an IDL file contains interface header and body. The interface header contains attributes and the interface body contains interface methods. See below:

// Hello.idl : IDL source for IHello

import "unknwn.idl";

interface IHello : IUnknown
	[id(1)] HRESULT SayHello([in] BSTR message);
  • Define an interface IHello using IDL language and save the file as “Hello.idl” file.
  • IUnknown interface is defined in “unknwn.idl” file. So, imported it into “Hello.idl” file; because this file is using IUnknown interface.
  • Inside interface header:
  • Inside interface body, define SayHello function. This function takes one argument (a string) and display the message.

Step (2). Now compile “Hello.idl” file using Microsoft’s MIDL compiler. Type the following at command prompt.

midl Hello.idl

MIDL compiler compiles “Hello.idl” file and generate the following files.

  • Hello.h – This is the header file contains the type definitions and function declarations for the interfaces.
  • Hello_i.c – The interface ID file. This file contains the IIDs and CLSIDs for every interface defined in IDL file. For eg: IID_IHello is defined in this file.
  • Hello_p.c – Proxy/Stub code contains in this file. Proxy/Stub code is used for marshaling. Marshaling is the process of packaging and unpackaging parameters between the remote procedure calls.
  • dlldata.c – Contains information to build and register a proxy/stub DLL.

Next article we will look into defining a component based on these MIDL generated files.


1 comment for “COM: Creating a COM Component using C++ (Part-1)

Leave a Reply