Basic COM  «Prev 

COM interface properties

this pointer as a COM interface pointer

The this pointer of a C++ class instance points to the internal class object built by the C++ compiler. A C++ class with virtual functions contains a vtable to access those functions. Because the vtable is the first object in the C++ class object, the this pointer is in effect a pointer to a pointer to a vtable. That is, it has the same binary layout as a COM interface.
Why we have to use the corresponding interface pointer as the this parameter.
There are of course other means to do that, as long as you are the implementer of the component.
But this is not the case for the clients of our component. When the component is built using the COM way, clients of our component know nothing about the internals of our component. Clients can only take hold of the interface pointer, and this is the very pointer that will be passed into the interface method as the this parameter. Under this expectation, the compiler has no choice but to generate the interface method's code based on this specific this pointer.
So the above reasoning leads to the result that:
It must be assured that each function in a vtable must recieve the corresponding interface pointer as its "this" parameter.

COM interface

To summarize, it is important to understand what a COM interface is, and is not:
  1. The pure virtual definition carries no implementation. If you are a C++ programmer, you can define your implementation of an interface as a class, but this falls under the heading of implementation details, which COM does not specify. An instance of an object that implements an interface must be created for the interface actually to exist. Furthermore, different object classes may implement an interface differently yet be used interchangeably in binary form, as long as the behavior conforms to the interface definition.
  2. A COM interface is not an object and is simply a related group of functions. A COM interface is the binary standard through which clients and objects communicate. As long as it can provide pointers to interface methods, the object can be implemented in any language with any internal state representation.
  3. COM interfaces are strongly typed. Every interface has its own interface identifier (a GUID), which eliminates the possibility of duplication that could occur with any other naming scheme.
  4. COM interfaces are immutable. You cannot define a new version of an old interface and give it the same identifier. Adding or removing methods of an interface or changing semantics creates a new interface, not a new version of an old interface. Therefore, a new interface cannot conflict with an old interface. However, objects can support multiple interfaces simultaneously and can expose interfaces that are successive revisions of an interface, with different identifiers. Thus, each interface is a separate contract, and systemwide objects need not be concerned about whether the version of the interface they are calling is the one they expect. The interface ID (IID) defines the interface contract explicitly and uniquely.