COM Interoperability With .NET Applications

Written by John DeVight on 2011-May-11

rating: 0+x

Overview

I am working on an application that needs to be able to talk to legacy COM objects. After a little bit of research, I had my ASP.NET application talking to the legacy COM objects.

Here are the steps that I followed:

  • Register the COM component using regsvr32
  • Locate the type library for the COM component
  • Generate a Runtime Callable Wrapper .NET assembly from the COM type library
  • Reference the Runtime Callable Wrapper .NET assembly
  • Instantiate and use the COM component through the Runtime Callable Wrapper .NET assembly

Register the COM component using regsvr32

Here is an example of the DOS command to register the COM component using regsvr32:

regsvr32 my-com-component.dll

Note: if you are using Windows Vista or Windows 7, remember to open the command line as an administrator.

Locate the type library for the COM component

The type library has a manifest; information about the objects and methods defined in the COM component. Since .NET cannot talk directly to a COM component, a Runtime Callable Wrapper (RCW) must be created to handle all communications with the COM component. The RCW is generated from the type library.

Generate a Runtime Callable Wrapper .NET assembly from the COM type library

To generate the Runtime Callable Wrapper, I used a tool called the TLBIMP.exe (Type library Importer). Here is an example of the DOS command to generate a Runtime Callable Wrapper .NET assembly from the COM type library:

tlbimp my-com-component.tlb /out:MyComComponent.RCW.dll

Reference the Runtime Callable Wrapper .NET assembly

Reference the Runtime Callable Wrapper .NET assembly as you would any other .NET assembly.

Instantiate and use the COM component through the Runtime Callable Wrapper .NET assembly

The namespace for the Runtime Callable Wrapper .NET assembly is based on the name of the .NET assembly. In my example above, the Runtime Callable Wrapper .NET assembly is called MyComComponent.RCW.dll. Therefore my namespace is MyComComponent.RCW. To identify the names of the objects and their methods, view the referenced Runtime Callable Wrapper .NET assembly with the Object Browser in Visual Studio. Now, simply use he COM objects and their methods as you would any other object in .NET.

Here is an example:

MyComComponent.RCW.MyObject obj = new MyComComponent.RCW.MyObject();
string myparam = "some value";
var myreturnvalue = obj.MyMethod(myparam);

References

Support ASP.NET Wiki

If you like this page, click on the "Share on" links in the wikidot toolbar at the top of the page to share it with your friends.

Comments

Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License