OOPortal OOPortal

Basic COM  «Prev  Next»
Lesson 14Type libraries
ObjectiveUse IDL to describe a COM object and build a type library.

COM Type Libraries

(IDL to describe COM object)

A COM interface pointer is a pointer to a vtable pointer, that is, a pointer to a pointer to a table of function pointers. The only way to communicate with a COM object is through its interfaces.
Given these properties, how can a Visual Basic client, using a high-level development language that does not provide access to pointers, access a COM object?
The designers of COM broke the solution into two parts:
First, they provided a language-independent way to describe COM objects and interfaces. Second, they provided a standard "front end" to act as a dispatcher between high-level development environments and COM objects.
In this lesson, we study how to describe COM objects and interfaces in a language-independent manner using type libraries. The standard front end is called (OLE) automation. This course does not cover automation.

Type libraries

A type library can be considered a type of binary include file. Interfaces and COM objects can be defined in a type library. Visual C++ has been extended to allow C++ programmers access to type libraries. The Visual C++ ATL COM-Wizard, covered later in this course, automatically generates type libraries.
Type libraries are described using IDL and compiled into binary format with MIDL. The following is an IDL file, MyComObject.idl, that describes MyComObject from the previous lessons and its IMyComInterface and IYourComInterface interfaces.
The first 16 lines of the MouseOver below use IDL to define COM interfaces in the same way we described earlier.
The remaining lines define a type library called MyTypeLib. Like COM interfaces and COM objects, type libraries are defined by a 128-character ID. For a type library, the ID is called a LIBID.

  1. This defines the LIBID for MyTypeLib.
  2. This denotes the start of a type library definition. Library definitions use the library IDL keyword. The definition is delimited by curly brackets ({}).
  3. This imports type descriptions from standard type library stdole32.tlb, which contains COM's predefined type descriptions (i.e., IUnknown, IID_IUnknown, etc.).
  4. COM objects can only be defined within the scope of a type library. This defines the CLSID for MyComObject.
  5. IDL keyword coclass starts the definition of a COM object. This starts the descriptions of MyComObject. Like interfaces and type libraries, the scope of a COM class description is delimited by curly brackets ({}).
  6. These place interfaces IMyComInterface and IYourComInterface within MyComObject.
COM Type Libraries
Compiling an IDL file with MIDL produces the *.h and *.c files described in Lesson 8, MIDL: Compiling IDL files. It also produces the type library MyComObject.tlb, which contains type library MyTypeLib.
The OLE/COM Object Viewer utility can be used to view the contents of a type library.

Ole Com Object Viewer - Exercise

Click the Exercise link below to explore some of the standard type libraries provided with Visual C++.
OLE COM Object Viewer - Exercise