What are Type Libraries?

A Type Library is a block of data which describes one or more COM Object Classes.  The internal format of the data is not important, because it is seldom accessed by application programs.  Typically, it is only accessed by COM Browsers such as PBROW.EXE (supplied with PowerBASIC), TypeLib Browser from Jose Roca, or OLEVIEW.EXE from Microsoft.  In the unusual circumstance that you must access this data directly, the Windows API provides numerous functions for just that purpose.

A Type Library is usually supplied by the author of the COM server. It's frequently supplied as a standalone data file with a file name extension of TLB.  The data can also be embedded as a resource in the associated DLL or EXE.  In practice, you would generally use a COM Browser to extract enough information about a COM Object to allow you to use these classes in your program.  Generally speaking, a Type Library usually supplies specific details about every METHOD and PROPERTY (function), and the parameters of each of them.  This would include the names, data types, return values, and more.  The Type Library may also offer information about related equates, User-Defined-Types, and more.  To include a numeric equate in your type library, just append the words AS COM to the equate definition:


Traditionally, it was common to use Interface Definition Language (IDL) to create the source code for the definitions you wish to describe in a Type Library.  IDL was created specifically for this purpose and resembles C++ syntax.  Once the source code was written, you would use Microsoft's MIDL Compiler to create the final Type Library.  That's a fairly cumbersome process.

With PowerBASIC, it's a bit simpler than that.  Whenever you create a COM server, simply add the #COM TLIB ON metastatement to your source and your Type Library will be created automatically. You can prevent a Type Library from being created by using the #COM TLIB OFF metastatement.  A Type Library is created with the same primary name as your COM server, and a file extension of TLB.  That is, if you create a COM server named XX.DLL, PowerBASIC will name the Type Library as XX.TLB.  The Type Library offers a description of every published class on the server.  You can then use any COM Browser to display the type information in a format that meets your needs.  The PowerBASIC COM Browser converts it directly to PowerBASIC source code declarations which can then be dropped into your COM client program.  If any of your Methods or Properties use data types not supported by Type Libraries, you will receive a Error 581 - Type Library creation error. If you wish to create a Type Library for you COM server, then only use data types that are compatible with Type Libraries, which are   BYTE, WORD, DWORD, INTEGER, LONG, QUAD, SINGLE, DOUBLE, CURRENCY, OBJECT, STRING, WSTRING, and VARIANT.

As mentioned earlier, you can consolidate your distribution files by embedding your Type Library right into your DLL or EXE as a resource.  A utility program named PBTYP.EXE is provided for just that purpose.  PBTYP.EXE is executed with one or two command line parameters used to specify the files to be used in the embedding process.  The syntax is:

PBTYP.EXE   TargetFile  [ResourceFile]

The PBTYP.EXE utility requires that you supply two or three files: the Target File (the DLL or EXE which receives the resource), the TypeLib File (the Type Library to be embedded), and optionally a resource file to be used.  Since it's assumed that the Target File and the TypeLib file share the same primary name, only the Target file name is needed.  If an extension is not supplied, the default of ".DLL" is used.  When executed, PBTYP.EXE scans the original resource file (such as ABC.RC), and replaces any references to a Type Library with a reference to the new Type Library.  It then compiles it to a resource object file (such as ABC.RES), and then creates a final PowerBASIC version (such as ABC.PBR).  Finally, it removes any prior resource from the target file, and replaces it with the newly created resource.  It should be noted that RC.EXE and PBRES.EXE must be present in your path for the process to complete.


See Also

What is an object, anyway?

Where are objects located?

Why should I use objects?

How do you publish an object?