Sunday, June 15, 2008

Dot net COM interoperability

"COM interoperability" a topic something important for a person coming from C++ / COM background to dot net world. Also a frequent question in dot net tech interviews -

I tried putting few test applications and knowing what it is.
In COM world the final output which we get is in the form of Typelibrary, a binary output.
While in dot net supported languages like C#, the output is know as Assembly, not binary but known as Intermediate Language (IL) output.

On a broad level, COM interoperability is transformation of these two different types of output files.
There could be 2 cases -

1. COM outputs to be used in dot net languages - We need to convert the TypeLibrary to assembly. It’s done by utility - TlbImp.exe - Type library import.

I never used this explicitly as there is simple option tab available while giving reference to COM dll in the dot net editor. It internally manages these all stuff of
Conversion.


2. Dot net dlls to be used in unmanaged environments - It requires conversion of Assembly to TypeLib. It could be done with - RegAsm.exe utility which stands for Register
assembly as Typelib. Honestly saying, I never required and used this.

This is what I could write, please share your experience.

Bye.
Avaneesh Tiwari

15-06-2008

2 comments:

Unknown said...

I have used Tlbimp.exe in one of my projects to carry out interactions between a COM dll and a .net app.

TlbImp.exe converts all the dispinterface functions(COM events) to .Net events with appropriate delgates and all the interface functions to .Net compliant functions.

The output of Tlbimp.exe is a dll called as RCW(runtime callable wrapper). This dll is nothing but a .Net version of the IDL file of the COM component.. that means it contains all the events and functions that the COM component exposes, but in the form of delegates, events and .net compliant arguments.

The .Net application that wants to use the COM component, should add the RCW file in its references. Once this is done the interfaces and coclass of the COM dll becomes available as any other .Net class.

All the events of the COM Dll can be handled in the same way as we handle an .Net event of any other class.


The process is as follows:
1. Create the COM dll.

2. Call the Tlbimp.exe from command prompt in following format
>Tlbimp YOURCOMDLL.dll OUTPUTRCW.dll

3. Once the RCW dll is created, use this file in your .Net project reference.

4. Now all the classes, interfaces and the events of the COM dll becomes availble in the .Net app.

5. Place the actual COM dll in your project folder.

One point to remember over here is that as RCW is only a wrapper on the actual COM component functions and events, it does not contain the actual implementation of these functions, hence we need to copy even the actual COM dll into the project folder.

The flow will be as follows:

.Net --> RCW --> COM
App <-- DLL <-- DLL

अवनीश एस तिवारी said...

Vibin,

Thanks for sharing this.

Regards
Avaneesh