C# Classes as COM Objects


COM Interoperability is the feature of Microsoft .Net that allows managed .Net code to interact with unmanaged code using Microsoft’s Component Object Model semantics. Most of the hoopla about COM Interoperability in .NET centers on the ability of .Net code to invoke legacy COM objects (such as ActiveX controls and COM objects encapsulated in DLLs).

But there exists the equally powerful and useful ability to develop new components in .Net and make those components accessible to unmanaged code as COM objects. That is the side of COM Interoperability that we discuss in this article.

This article assumes that the reader has a basic familiarity with COM and .Net terminology.

.Net Interfaces and Classes

The basis for accessing .Net objects either from other .Net code or from unmanaged code is the Class. A .Net class represents the encapsulation of the functionality (methods and properties) that the programmer wants to expose to other code. In C++, classes supported multiple inheritance from other classes. .Net doesn’t support multiple inheritance, but does support implementation of multiple interfaces. A .Net interface is the abstract declaration of the methods and properties that classes which implement the interface are expected to provide in their implementations. Declaring a .Net interface doesn’t generate any code, and a .Net interface is not callable directly. But any class which implements (“inherits”) the interface must provide the code that implements each of the methods and properties declared in the interface definition.

Example 1: C# Interface and Class Definitions

interface IDotNetInterface
{
void Initialize();

string Caption
{
set;
}

int ShowDialog(string sText);
}

class DotNetClass : IDotNetInterface
{
// Need a public default constructor for COM Interop.

public DotNetClass()
{
}

public void Initialize()
{
m_sCaption = “”;
}

public string Caption
{
set { m_sCaption = value; }
}

public int ShowDialog(string sText)
{
System.Windows.Forms.MessageBox.Show(sText, m_sCaption);
}

private string m_sCaption;
}

Although a .Net class is not directly invokable from unmanaged code, Microsoft has provided the capability of wrapping the .Net interface in an unmanaged layer of code that exposes the methods and properties of the .Net class as if the class were a COM object. There are two requirements for making a .Net class visible to unmanaged code as a COM object:

Requirement 1: Creating a Description of the COM class and Interfaces

For a client of a COM object to have access to the object, the client needs a description of the object — how to locate it and how to call its methods and properties. For a “real” unmanaged COM class, this description is available in the form of a Type Library — a binary description of the GUIDs, classes, and interfaces (methods, properties, and parameters) that the COM class supports.

.Net assemblies don’t include information in Type Library compatible format. So it is necessary for the programmer to run one of two .Net-supplied utilities to extract the assembly description of a class into a Type Library file. One utility is TLBEXP.EXE, the .Net Type Library Exporter. This command line utility takes as input the name of an assembly DLL file to be converted to a Type Library. The programmer can also specify the name of a Type Library file to be created.
Continues…

Pages: 1 2

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!