Next: , Previous:   [Contents][Index]


2 What are COM Objects and Interfaces?

COM objects are collections of public interfaces and private data. The internal representation of the COM object is irrelevant as clients of COM objects can only access interfaces. Win32 provides APIs for creating an instance of a COM object and requesting the first interface to the object. Clients are not required to specify the location of the COM objects as the operating system will resolve their location and take the needed steps to create the object, but the location or hints to the location may be specified if needed.

Every COM object must support at least one interface called IUnknown. Depending on the particular interfaces supported in addition to IUnknown, the object is referred to by different names in Microsoft documentation. For example if the object supports the interface IDispatch it is called an OLE Automation object, if it supports the various GUI related interfaces and persistence interfaces, it may be called an OLE, OCX, or ActiveX control.

All interfaces and COM objects are identified using a GUID, a globally unique identifier. These IDs are created by the Win32 API function CoCreateGuid, or assigned by Microsoft. The GUIDs referring to interfaces are called IIDs, interface identifiers, those referring to the COM object as CLSIDs, class IDs. If and object has registered a textual name, called a ProgID, for itself in the Windows registry, it is possible to retrieve the CLSID using the ProgID with the API function CLSIDFromProgID.

An interface is a record containing an access to a collection of function accesses. The collection is usually implemented as a record with access to subprogram objects, and private data. The first parameter of the function is always an access to the interface it belongs to and usually returns an HRESULT, an unsigned integer that returns success or failure information. An interface is said to derive from another interface if it contains a superset of another interface’s collection of function accesses in the same order.

The most important interface is called IUnknown and all other interfaces derive from it. It contains three functions QueryInterface, AddRef, and Release. These three functions provide polymorphism and reference counting. The QueryInterface method is called on any interface to request the COM object to return an access to another supported interface. AddRef and Release provide reference counting for the interface. An implicit AddRef exists when an interface is returned from a function such as QueryInterface or through API calls. Release is called by the client when the interface is no longer in use reducing the interface’s internal reference count. When the reference count reaches zero for all interfaces to the COM object, the COM object is required to deallocate itself from memory.


Next: , Previous:   [Contents][Index]