List the binary layout requirements of a COM interface.
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.
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.
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.