![]() |
![]() ![]() |
|
COM object essentials Continued from Introduction What's inside a COM object? That's none of your business! A COM object shares information with the world only through defined interfaces. Each interface provides one or more functions that another object or a program can call. Every COM object must support the IUnknown interface and its three functions, AddRef, Release, and QueryInterface. AddRef and Release handle the mundane task of managing the object's life span. Each call to AddRef increments the object's reference count, and each call to Release decrements the reference count. The object is destroyed when the reference count goes to 0. The utterly essential function of the IUnknown interface is QueryInterface. Once a program or another object gets access to the always present IUnknown interface, it can call QueryInterface to access all other interfaces supported by the object. IUnknown is the root of all COM interfaces. Every other COM interface is effectively a descendant of IUnknown and thus must also provide implementations of the three IUnknown functions. The concept of object in COM terminology isn't quite the same thing as the concept as it relates to Delphi or C++. A COM interface, however, is similar to a Delphi or C++ object that has no public data members and only virtual methods. The interface's list of functions corresponds directly to an Object Pascal or C++ object's virtual method table. In fact, you can create a COM interface in either language simply by declaring an object with the correct list of virtual methods. The method declarations must match the function definitions for the desired interface, naturally, but they must also appear in the correct position in the virtual method table. That means the methods must be declared in the defined order and no other virtual methods can precede them. The OLE2.PAS file that comes with Delphi 2.0 defines an IUnknown interface object type and dozens of IUnknown descendants, such as IClassFactory, IMarshal, and IMalloc. Each of the supplied objects' interface function methods is declared as virtual, stdcall, or abstract. The virtual keyword is required, as noted above. The stdcall keyword instructs the compiler to use the standard calling convention for the method. The abstract keyword indicates that the method is not implemented in the object itself but must be implemented in any descendant for which an instance is created. More than 50 direct descendants of IUnknown are defined in OLE2.PAS, and each one supports both the interface for which it is named and IUnknown. A problem arises when a COM object needs to support two or more interfaces other than IUnknown. The C++ implementation simply defines the COM object as multiply inheriting from the objects representing the interfaces it needs to support. An object in Delphi doesn't support multiple inheritance, so a different approach is required. (C++ programmers may be interested to note that an MFC-based COM object uses the same approach as described below for Delphi, a fact obscured by the complex set of macros used to define a COM object with MFC.) Published as Power Programming in the 01/07/97 issue of PC Magazine. |
|
TOP | ![]() Copyright (c) 1997 Ziff-Davis Inc. |