Wednesday, July 11, 2007

How to Import a Type Library from the Command Line

I am kind of biased but I think one of the most useful utilities in the Delphi or C++Builder bin directory is tlibimp.exe. In case you don't know what tlibimp.exe does, it allows you to use COM objects as if they are native interfaces, and .NET objects can be treated as COM objects, so there is a lot of potential here. This is the command line version of the Import Component Wizard, but who wants to use the IDE when we've got command line!

One of the most common problems users of Delphi run into only a few options are available from the IDE. Here is a list of all the options provided by tlibimp:

Borland TLIBIMP Version 11.0
Copyright (c) 1997-2005 Borland Software Corporation
Syntax: TLIBIMP [options]

Files to Generate (Required):
-C- C++ Import file
-P- PASCAL Import file
-Ic- CORBA IDL file
-Ip- PASCAL CORBA stubs & skeletons

Customize code generated:
-Ha+ Create IDE component for Controls
-Hs+ Create IDE component for Servers
-Hr+ Generate component registration
C++ options:
-Cd- Generate dispinterfaces
-Cm- Map disp interfaces to dual
-Cn Set namespace name
-Cs+ declspec(__selectany) for GUIDs
-Ct+ Force the use of a _TLB file
-Cu+ Expose namespace with 'using'
-Cv+ BCB4-style server events
-Cw- Use disp. in Control wrappers

Ignore Flags: -Ya- All special flags,
-Yh- [Hidden]

Debug Build Options:
-G+ Show Debug output
-Gr Read & write out type library
-It IDL file parser test

Output File Options:
-D Output directory path
-Fe TLB suffix (-Fe- none)
-Ft TLB filename (no suffix)
-Ce OCX suffix (-Ce- none)
-Co OCX filename (no suffix)
-Hpa Set palette name
-Hps Set palette name

PASCAL options:
-Ps+ Map dual HRESULT to safecall
-Pt- Map all HRESULT to safecall

MISC options:
-O+ Generate CoClassCreator wrappers
-R+ Process dependent type libraries
-XM- Use MS-style getter/setter
-W+ Emit warnings in files
-Wc+ Emit comments in files
-Yc+ [CanCreate],
-Yp- [Predefined], -Yr- [Restricted]

-Pu Generate Pascal UI file
-Xd+ Generate DispInterfaces
-Yi+ Ignore Standard Type Libraries

That's a lot of options. Some options are obsolete such as the CORBA options but we leave them in case they are being used.

Internally Delphi uses a lot of code generated by tlibimp to communicate to the .NET services such as MSBuild, the .NET designers, and a lot of internal services. The command line we use is:

tlibimp.exe -P+ -Ha- -Hr- -Hs- -R- -D .tlb


  • -P+ to generate Pascal
  • -Ha- to not generate IDE components for Controls
  • -Hr- to not generate component registration
  • -Hs- to not generate IDE components for Servers
  • -R- to not process dependent type libraries
  • -D to generate the .pas files to the specified directory
  • this is the type library to import

Note that the -Yc option has changed from off to on in Delphi and C++Builder 2007. For a little history CanCreate flag is typically used in Visual Basic libraries and defaults to off. This means that VB objects aren't meant to be created. But after years of bug reports I decided to change the default of this option to on so now we ignore the flag.

3 comments:

Shawn Oster said...

There wouldn't happen to be a way to turn off the insertion of the date in the header would there? Subversion constantly thinks my tlb.pas file has changed because, well, it has, but the *only* thing different is the date up in the header.

Granted it's not a huge deal but every time I see that I get just a little more more annoyed :)

Great post by the way. I wrote a gui utility awhile back that wraps up the command-line version because the ide-based version would go really wonky on me. No, I have no idea why I wrote a gui version instead of just scripting the command line but hey, it was a learing experience :)

Chris Bensen said...

Hi Shawn,

There is no option from the IDE. From the command line you can run -Wc- to turn off comments. If would be good to expose the command line options to the IDE.

Shawn Oster said...

Ahh, that makes sense when I actually think about it :) It would be nice to get some more control over the options the IDE uses. Seems msbuild could be brought into play here somehow to help with that but I'm talking off the cuff.

Post a Comment