CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

COM – Creating a COM Component using C++ – IUnknown interface

COM – An introduction

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 only 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 Components expose the interfaces to COM Clients (those who are going to use COM Components) or other 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 a variety of programming languages. In this article, we are going to create a simple COM component using the C++ programming language. As this concept is difficult to understand, we are going to explain this in a series of articles; instead of a single article.

Before going to start implementing a COM component, let’s have a look at the basic interface IUnknown.

The 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 the QueryInterface method is used to retrieve the pointer to the object’s interfaces. Reference count controls the life of the object. When no references to the object, the object should be destroyed.

Now is the time for implementation.

IDL file (.idl)

Step (1). The first step is to define an interface using Interface Definition Language (IDL) and save it in the .idl file. Each interface in an IDL file contains an 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";

[
	uuid(2BEFC176-884D-4B33-A9FB-B0F86F2699A5),
	version(1.0)
]
interface IHello : IUnknown
{
	[id(1)] HRESULT SayHello([in] BSTR message);
};
  • Define an interface IHello using IDL language and save the file as a “Hello.idl” file.
  • IUnknown interface is defined in “unknwn.idl” file. So, imported it into the “Hello.idl” file; because this file is using the IUnknown interface.
  • Inside interface header:
    • The uuid interface attribute is used to define a unique 128-bit identifier for this interface.  The uuid value can be generated by using GUID generator tools.
    • The version attribute is used to specify the version number of this interface.
  • Inside the interface body, define the SayHello function. This function takes one argument (a string) and displays the message.

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

midl Hello.idl

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

  • Hello.h – This is the header file containing 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.

**

COM – Creating a COM Component using C++ – IUnknown interface

2 thoughts on “COM – Creating a COM Component using C++ – IUnknown interface

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top
Exit mobile version