Using .NET Classes from COM

Exposing your .NET classes to COM is easy. All you have to do is decorate the class, member, or assembly with the ComVisible attribute and that will be enough for the registration tool to determine what information to include in the type library when it is created. To follow along with the samples you are about to see, just create a new class library called ComVisibleLibrary.

The easiest way to expose your code to COM is to expose your entire assembly to COM. You can do this by manually editing your AssemblyInfo.cs file, or you can right-click your project, choose Properties, and then click the Assembly Information button. You will see a dialog like the one shown in Figure 13.3. Just check the checkbox indicating that the assembly is visible to COM.

Figure 13.3. Exposing an entire assembly to COM.

To test this out, change the Class1.cs class definition in the ComVisibleLibrary class library from the stock definition to the following:

using System; using System.Runtime.InteropServices; using System.Collections.Generic; using System.Text; namespace ComVisibleLibrary { [ComVisible(true)] public class ComVisibleClass { public void DoSomething() {     Console.WriteLine("Hello from within a COM object"); } } } 

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, as shown in Figure 13.4, 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.

Figure 13.4. Referencing a COM object from a .NET project.

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.

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: