OOPortal OOPortal

Basic COM  «Prev 

IUnknown reference counting

Implementation of QueryInterface, AddRef, and Release

 Line 14:AddRef, called from QueryInterface, increments m_refcnt.
 Line 23: Release decrements m_refcnt. If its value goes to 0, Release deletes the instance of CMyComObject and returns 0. A reference count of 0 indicates that the COM object and its interfaces are no longer in use.

Connecting to other Components: Interfaces

A single component by itself is not very useful. So, to let objects talk to each other, components include interfaces. An interface is the class plus one or more methods (and the method's associated parameters). As a naming convention, every interface is designated I "Something" in Figure 2-12 the interfaces are IUnknown, IReservation, and IAuction.
Figure 2-12: Interfaces are IUnknown, IReservation, and IAuction.


Every COM component has an IUnknown interface. In schematic representations of components, like Figure 2-12, the IUnknown interface is always the lollipop that sticks straight out of the top of the component. The IUnknown interface makes the component self-describing. For example, a COM client might need a COM server with the IReservation interface. The client can make a system- or network-wide request to all components through their IUnknown interfaces, asking
"Do you have an IReservation interface?"
The first component responding to the query handles the IReservation client request.

Other interfaces

In addition to IUnknown, a COM component also has at least one other interface. In schematic representations of components, like Figure 2-12, these interfaces are the lollipops that stick out of the left side of the component. If you have used Visual Basic (VB), you might also be familiar with the IDispatch interface. IDispatch, used exclusively by VB clients, is similar to (but not as efficient as) IUnknown.

Making components real: creating classes

When you create a component by compiling it and turn it into machine code and you have created a class. A class is an OO term to describe a collection of methods and attributes. In COM, you define the class using MIDL (Microsoft Interface Definition Language) and tag it with a GUID to identify the class uniquely.