Pages

2013-04-03

DotNet: Using .NET Classes from COM - Part 2

After you have compiled an assembly that is visible to COM, it isn't automatically made available to COM clients. To make your code available to COM clients, you need to register your COM objects in the registry in a method similar to the way you would register your unmanaged C++ or VB6 COM objects. To register a .NET-hosted COM object, you use the regasm command-line SDK tool, as shown in the following example:


regasm ComVisibleLibrary.dll /tlb:ComVisibleLibrary.tlb

This will not only register your library with all the other COM objects on the machine, but it will also export a usable type library based on your assembly.

You might think that when you have a .NET COM object registered, you can consume that .NET COM object from managed code. If you try to reference your own COM object from within .NET, you will receive an error message indicating that you cannot create a COM reference to a .NET assembly. This is, of course, for a good reason. If there is a .NET assembly available, there is no need for managed code to use COM, as it would add a lot of unnecessary overhead. 

To reference your .NET-hosted COM object from an unmanaged language, simply use whatever tools you would normally use to consume unmanaged COM components to reference the .NET-hosted component. The COM-Callable Wrapper that .NET places between the unmanaged COM code and your managed code abstracts the fact that it is a .NET object. 

The benefit of this is that unmanaged COM clients can use .NET-hosted COM objects and regular/legacy COM objects interchangeably without having to do any additional work.


No comments:

Post a Comment