Go Up to COM basics Index
Type libraries provide a way to get more type information about an object than can be determined from an object's interface. The type information contained in type libraries provides needed information about objects and their interfaces, such as the interfaces that exist on specific objects (given the CLSID), the member functions that exist on each interface, and the arguments required by those functions.
You can obtain type information either by querying a running instance of an object or by loading and reading type libraries. With this information, you can implement a client that uses a desired object, knowing specifically what member functions you need, and what to pass those member functions.
Clients of Automation servers, ActiveX controls, and transactional objects expect type information to be available. All of Delphi's wizards generate a type library automatically, although the COM object wizard makes this optional. You can view or edit this type information by using the GenTLB.exe utility to create a .tlb file from a RIDL file.
The content of type libraries
Type libraries contain type information, which indicates the interfaces that exist in the various COM objects, as well as the types and numbers of arguments to the interface methods. These descriptions include the unique identifiers for the CoClasses (CLSIDs) and the interfaces (IIDs), so that they can be properly accessed, as well as the dispatch identifiers (dispIDs) for Automation interface methods and properties.
Type libraries can also contain the following information:
- Descriptions of custom type information associated with custom interfaces
- Routines that are exported by the Automation or ActiveX server, but that are not interface methods
- Information about enumeration, record (structures), unions, alias, and module data types
- References to type descriptions from other type libraries
Creating type libraries
With traditional development tools, you create type libraries by writing scripts in the Interface Definition Language (IDL) or the Object Description Language (ODL), then running that script through a compiler. However, Delphi automatically generates a type library when you create a COM object (including ActiveX controls, Automation objects, remote data modules, and so on) using any of the wizards on either the ActiveX or Multitier page of the new items dialog. You can also create a type library by clicking File > New > Other, selecting the ActiveX folder under Delphi Projects or C++Builder Projects, and in the right pane choosing Type Library.
Note: Type libraries on RAD Studio use RIDL instead of IDL.
You can view and edit the type library using Delphi's Type Library Editor. Delphi automatically updates the corresponding
.tlb file (binary type library file) when the type library is saved. For any changes to Interfaces and CoClasses that were created using a wizard, the Type Library editor also updates your implementation files.
When to use type libraries
It is important to create a type library for each set of objects that is exposed to external users, for example,
- ActiveX controls require a type library, which must be included as a resource in the DLL that contains the ActiveX controls.
- Exposed objects that support vtable binding of custom interfaces must be described in a type library because vtable references are bound at compile time. Clients import information about the interfaces from the type library and use that information to compile. For more information about vtable and compile time binding, see Automation Interfaces.
- Applications that implement Automation servers should provide a type library so that clients can early bind to it.
- Objects instantiated from classes that support the IProvideClassInfo interface, such as all descendants of the VCL TTypedComObject class, must have a type library.
- Type libraries are not required, but are useful for identifying the objects used with OLE drag-and-drop.
When defining interfaces for internal use only (within an application) you do not need to create a type library.
Accessing type libraries
The binary type library is normally a part of a resource file (.res) or a stand-alone file with a .tlb file-name extension. When included in a resource file, the type library can be bound into a server (.dll, .ocx, or .exe).
Once a type library has been created, object browsers, compilers, and similar tools can access type libraries through special type interfaces:
Special Type Interfaces:
Provides methods for accessing a library of type descriptions.
Augments ITypeLib to include support for documentation strings, custom data, and statistics about the type library.
Provides descriptions of individual objects contained in a type library. For example, a browser uses this interface to extract information about objects from the type library.
Augments ITypeInfo to access additional type library information, including methods for accessing custom data elements.
Provides a fast way to access information that compilers need when binding to an interface.
You can import and use type libraries from other applications in Delphi by clicking Component > Import Component. Most of the VCL classes used for COM applications support the essential interfaces that are used to store and retrieve type information from type libraries and from running instances of an object. The VCL class TTypedComObject supports interfaces that provide type information, and is used as a foundation for the ActiveX object framework.
Benefits of using type libraries
Even if your application does not require a type library, you can consider the following benefits of using one:
- Type checking can be performed at compile time.
- You can use early binding with Automation, and controllers that do not support vtables or dual interfaces can encode dispIDs at compile time, improving runtime performance.
- Type browsers can scan the library, so clients can see the characteristics of your objects.
- The RegisterTypeLib function can be used to register your exposed objects in the registration database.
- The UnRegisterTypeLib function can be used to completely uninstall an application's type library from the system registry.
- Local server access is improved because Automation uses information from the type library to package the parameters that are passed to an object in another process.
- GenTLB.exe is a utility provided by Embarcadero that generates a .tlb file from a RIDL file (an intermediate text-based file used by the Type Library Editor).
Using type library tools
The tools for working with type libraries are listed below.
- TLIBIMP.EXE (Type Library Import tool), which takes existing type libraries and creates Delphi Interface files (_TLB.pas files), is incorporated into the Type Library editor. TLIBIMP provides additional configuration options not available inside the Type Library editor.
- TRegSvr.exe is a tool that ships with Delphi, for registering and unregistering servers and type libraries. The source to TRegSvr is available from Start | Programs | Embarcadero RAD Studio | Samples in the Delphi\COM directory.
- RIDL (the Embarcadero Restricted Interface Definition Language)
- MIDL (the Microsoft IDL compiler) compiles IDL scripts to create a type library.
- RegSvr32.exe is a standard Windows utility for registering and unregistering servers and type libraries.
- OLEView is a type library browser tool, found on Microsoft's Web site.