The .NET Framework SDK Type Library Importer (Tlbimp.exe) is a tool that exposes a COM type library as an assembly called an interop assembly. This assembly defines managed equivalents, or wrappers, for each COM interface in a given type library.
When methods of the interop assembly are called, a managed-to-unmanaged transition is executed and control is passed to the COM component. Likewise, when the unmanaged COM function returns, an unmanaged-to-managed transition is executed. By default, the COM HRESULT is checked for failure, and an exception is thrown if the HRESULT does not indicate success. Similarly, COM component initialization and interface queries are performed by the interop assembly and therefore hidden from the calling code.
Interop assemblies do not replace the COM components they represent; the unmanaged COM functions remain in the COM component, so the component must be installed and registered on target computers or calls to the interop assembly fail.
Using Tlbimp is the easiest way to use a COM component from managed code, but there are some serious disadvantages, especially for large and/or complex COM interfaces. These disadvantages are:
-
Tlbimp
generates managed interfaces for every COM interface in the type library. There is no way to suppress this behavior, so the resulting assemblies can be very large. (For example, the Tlbimp-generated interop assembly for Mshtml.dll is more than 8 MB.) There is also no way to hide an interface that is meant only for use within the COM component.
-
Tlbimp
supports a limited number of data types. Usually unsupported types are imported into the managed world as the generic, non type-safe IntPtr type, requiring error-prone and tedious marshaling code to use the assembly, but sometimes Tlbimp.exe cannot expose members of an interface at all.
-
Tlbimp
generates a separate interop assembly, which it must be deployed with the final application.
If these disadvantages are acceptable, see How to: Use Native COM Servers with TLBIMP for an example.