Pages

2013-04-03

DotNet: Using .NET Classes from COM - Part 1


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 below. Just check the checkbox indicating that the assembly is visible 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");
}
}
}

No comments:

Post a Comment