The .NET Framework provides many tools for runtime as well as Visual Studio .NET (VS .NET) users. We'll use VS .NET for the applications in this chapter, but it doesn't hurt to take a look at these options. It's easy to convert a COM library into a .NET assembly using these tools. There are three different ways to do so.
VS .NET provides a pretty simple way to import a type library in a managed application. The Project Add Reference menu option allows you to access a COM library and add it to the project. This adds a namespace to the project, so you can access the library members through that namespace.
The Type Library Importer, known as Tlbimp or Tlbimp.exe, is a command-line utility to convert a COM type library into a .NET assembly. Why a command-line utility? Well, this is an obvious question if you're using VS. You can ignore this option if you use VS.NET for your development. This option is useful for developers who don't have VS.NET.
Tlbimp.exe takes as a minimum one argument, the filename of the COM type library that you want to convert to an equivalent .NET Runtime assembly. The output of Tlbimp.exe is a binary file that contains runtime metadata for the types defined in the original COM type library.
This is the syntax:
tlbimp tlbFile [options]
where tlbFile is the name of the COM type library.
Table 20-1 describes some of the Tlbimp.exe options.
OPTION | DESCRIPTION |
---|---|
/asmversion:versionNumber | Specifies the version of the assembly to produce |
/help | Displays the available help commands |
/namespace | Specifies the namespace in which to produce the assembly |
/out:filename | Specifies the name of output assembly |
/references:filename | Specifies the namespaces to use to resolve references to types defined outside the current type library |
/? | Displays syntaxes and options for the tool |
You can find Tlbimp.exe in the FrameworkSDK\Bin directory of your VS .NET or .NET Software Development Kit (SDK) installation.
The Type Library Exporter (Tlbexp.exe) does the reverse job of Tlbimp.exe. It generates an equivalent type library for a .NET assembly. This utility is useful when you need to use a managed code assembly in unmanaged code. Similar to Tlbimp, this utility also has options. The default command is as follows:
tlbexp assemblyName
where assemblyName is the name of the assembly for which you want to convert an equivalent type library.
Table 20-2 describes some of the options.
OPTION | DESCRIPTION |
---|---|
/help | Shows command syntax and options. |
/out:file | Specifies the name of the type library to generate equivalent to an assembly. If there are no options, the default name of the type library is the same as the assembly. |
/? | Shows command syntax and options. |
You can find Tlbexp.exe in the FrameworkSDK\Bin directory of your VS .NET or .NET SDK installation.
Caution | You can't use Tlbexp.exe to produce a type library from an assembly that was imported using Tlbimp.exe. |