Basic COM  «Prev  Next»
Lesson 6 COM interface properties
Objective List the binary layout requirements of a COM interface.

COM Interface Properties (

List the binary layout requirements of a COM interface

vtable or virtual table

A COM interface contains a group of related COM methods. COM interfaces have the following properties. The runtime binary layout of a COM interface contains vtable. A vtable is a table of pointers to interface methods.
A COM interface pointer is a pointer to a vtable.
Methods in a vtable are accessed by table position, not by method name. The placement of functions in a vtable is called vtable order.
Characteristics of a vtable
Characteristics of a vtable


Besides providing application-specific functionality, a COM interface must also provide methods to support interface navigation and lifetime management. These methods are QueryInterface, AddRef, and Release. Collectively, these three methods are called IUnknown. The first three pointers in every vtable associated with a COM interface must point to the IUnknown methods (i.e. QueryInterface, AddRef, and Release) in order.


COM does not support implementation inheritance. Unlike C++, in COM there is no way to inherit member functions and member variables from a base class. However, the standard convention used in COM is to say, "All COM interfaces inherit from IUnknown." What we really mean is that all COM interfaces implement the methods of IUnknown in vtable order, i.e., QueryInterface, AddRef, and Release are the first three functions in every COM interface.

C++ and virtual member functions

C++ is the language of choice for developing COM applications. One of the reasons for this is that C++ compilers place virtual member functions of a class into a vtable.

Defining COM interface

The following code declares a C++ class that, when instantiated, implements a COM interface called IMyComInterface:

class CIMyComInterface {

 //For C++ we usually add
 //a 'C' in front of the
 //interface name

    virtual HRESULT __stdcall QueryInterface
 (const IID& iid, void **ppv);
    virtual unsigned long __stdcall AddRef();
    virtual unsigned long __stdcall Release();

    virtual HRESULT __stdcall Fx1(CHAR *buf);
    virtual HRESULT __stdcall Fx2();

When a pointer to this class is passed to a COM client, the client can't see any part of the object other than the vtable.
Data and nonvirtual member functions are not visible or accessible.

Recall from the previous lesson that the first parameter to a COM interface method must be a pointer back to the interface that contains it. C++ provides automatic support for this requirement by passing in the this pointer as an implicit first parameter.


COM interfaces are identified by a unique ID called an IID.

Generating IIDs

A predefined macro called DEFINE_GUID is used to define IIDs as variables in your code. Microsoft provides a tool called guidgen to generate IIDs and DEFINE_GUID macros.
The guidgen tool
COM guidgen tool

Guidgen will generate a DEFINE_GUI macro for you. Click the New GUID button to generate a new GUID. Click the Copy button to copy the DEFINE_GUID macro with the generated IID copied into the clipboard. Paste the DEFINE_GUID macro into your code. Replace <<name>> with the interface name. For example:

0xc4cf2171, 0x4832, 0x11d2, 0x85, 0xbc, 0x8,
0x0, 0x17, 0x0, 0xc5, 0x7f);

Replace <<name>> with the name of your interface:
0xc4cf2171, 0x4832, 0x11d2, 0x85, 0xbc, 0x8,
0x0, 0x17, 0x0, 0xc5, 0x7f);
An IID (interface ID) is a 128-bit number. IIDs are named after the interface they identify. For example, IUnknown has a predefined IID called IID_IUnknown. IIDs are also called GUIDs or UUIDs.

Defining Com Interface - Exercise

Click the Exercise link below to apply what you have learned about COM interface requirements.
Defining Com Interface - Exercise

COM Programming with Microsoft .NET