Using COM Interop in the iWay .NET Technology Adapter

The code that operates within the Microsoft .NET Common Language Runtime (CLR) is called managed code. Managed code has access to all of the services provided by the CLR, such as security, versioning support, garbage collection, and cross-language integration. The iWay .NET Technology Adapter is implemented by managed codes. Code that does not operate within the CLR is called unmanaged code. Managed components depend on the CLR and expect other components with which they interact, to depend on the CLR as well.

In order for the iWay .NET Technology Adapter to use a COM object, wrapper objects, called Runtime-Callable Wrappers (RCW), must be generated. The RCW objects cater for the difference in lifetime management between .NET and COM. RCW objects are .NET objects that manage the reference count of a COM object, as well as deal with the organization of parameters and return types for the COM object methods.

RCW objects are manufactured at runtime by the CLR using information found in an Interop Assembly, which is an assembly containing definitions of COM types that can be used from managed code.

The simplest way to generate an Interop Assembly is to use the Type Library Importer tool (TlbImp.exe), which is a command-line tool provided by the Microsoft .NET Framework SDK. The Type Library Importer tool converts a COM type library into .NET Framework metadata, effectively creating a managed wrapper that can be called from any managed language.

You can select many options using the Type Library Importer tool. The most important option is /out, which allows you to specify a name for the resulting .NET assembly. The following command line converts a COM component called comp.dll to a matching .NET assembly called interop.NETcomp.dll:

tlbimp comp.dll /out: interop.NETcomp.dll

You can also use the Microsoft Intermediate Language (MSIL) Disassembler tool (Ildasm.exe) to examine the resulting DLL. The MSIL Disassembler tool is also a command-line tool provided by the Microsoft .NET Framework SDK. This tool parses any .NET Framework .exe or .dll assembly, and shows the information in a legible format. The MSIL Disassembler tool shows more than just the MSIL code, it also displays namespaces and types, including their interfaces.

For example, to display the contents of the interop.NETcomp.dll file, use the following command:

Ildasm interop.NETcomp.dll

After creating an Interop Assembly for an unmanaged COM component, you can run the assembly just like running other C# components in the iWay .NET Technology Adapter.


iWay Software