How to deal with TypeLibs in freepascal

Recent versions of freepascal for Windows can work with type libraries. It is not as comfortable as it seems to be in Delphi but it works. As far as I know you should use a version of freepascal 2.5 or above.

Typelibs are necessary for e.g. automating Office.

Convert tlb information to pascal unit

Unfortunately whether freepascal nor its RAD tool lazarus come with an editor for type libraries. To access the information typelibs provide you have to take detour. The following describes briefly how to go:

convert typelib to IDL format

  1. Get Ole Viewer from Microsoft. You can download it for free - it's part of the Windows 2000 Resource Kit (I know we're in 2010 currently). Install it. Execute it.
  2. Got to Menu File / View TypeLib...
  3. Choose .tlb, .olb, .exe or .dll file to open
  4. Save TypeLib as C++-Interface-Definition-Language file with Menu File / Save As...

Now you have the content of a typelib in text format.

convert IDL file to pascal TLB unit

I've written a little tool idl2pas to convert the interface information to a pascal-style unit. Follow the information on the idl2pas conversion tool site to download and install it.

C:\idl2pas> idl2pas.exe typelib.idl

This will create several files (assumed the name of the library - not the file name - is demolib):

  • demolib_tlb.pas: The main unit file you can use in your program.
  • demolib_types.inc: If the typelib includes type information you can find it here. It is a good idea to have it a seperate file because sometimes you only need the types rather than the interface definitions.
  • demolib_interfaces.inc: forward declarations of interface, dispinterface and coclass declarations.
  • demolib_implementations.inc: implementation of coclasses.
  • demolib_README.txt: If errors are found while converting they will be saved in this file. Especially missing types are noted here (can be a lot in a large typelib). If everything went fine you can delete this file.

If you like you can put all together in one file (the Delphi style) for future use.

fine tuning

After the actual conversation you have to put your hands on the files. Typical tasks are:

  • Clarify missing types from dependent units. The main reason types are missing is the absence of related libraries. For instance if you convert Outlook then types from Office are missing. If you resolve all dependencies and there're still types missing, you're on your own (unfortunately this is not unlikely). 
  • Correct the uses clause to mention all tlb units. You can play around here. Maybe only the type definitions of this unit are needed; in this case it's enough to include the _types.inc file.
  • I don't know whether typelibs can be incorrect. I know that after conversion not every line seems logical. Evaluate defect functions yourself. If you don't need them just comment it out.

 

Feel free to leave a comment. If your topic is related to the conversation tool idl2pas please do so on its own site. Remarks to using typelibs with freepascal or converting them for use with freepascal are welcome here.

Geben Sie einen Kommentar ab

Kommentare

Bisher hat niemand diese Seite kommentiert.

RSS Feed für die Kommentare auf dieser Seite | RSS feed für alle Kommentare