COM and .NET

Doesn't .NET replace COM?

Er, well, sort of.  The concepts behind .NET and CLR do make COM redundant in some ways.  However, there's a huge base of COM components out there, and .NET would never take off if you couldn't interoperate between COM and .NET.  So, you can use COM components from within .NET programs, and also use .NET assemblies from within COM programs.

COM Callable Wrapper (CCW)

In order to use a .NET assembly as a COM component, you use a utility called regasm.  This utility adds entries to the registry from information contained within the assembly metadata, and can optionally generate a registry file or type library.  Once the entries have been created, the assembly public classes can be used as if they were a normal COM class.

The regasm tool creates entries in the registry so that when a client instantiates the assembly as a COM object, the .NET runtime handles all the necessary implementation to allow this to happen.  If you examine the registry file created by invoking the regasm utility in a similar way to the example below, you will see that the execution engine (MSCorEE.DLL) is called instead of the actual library.  The following shows an example of the regasm utility being used:

D:\Temp\RegAsmExample>regasm MathsFunctions.dll /RegFile:MathsFunctions.Reg
Microsoft (R) .NET Framework Assembly Registration Utility 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Registry script 'D:\Temp\RegAsmExample\MathsFunctions.Reg' generated successfully

Another utility which can also be useful is the tlbexp utility - this exports a Type Library (.tlb file) from the metadata contained within the .NET assembly which can be imported into environments which support early binding (e.g. Visual Basic or Word/Excel etc which support VBA).  An example of invoking the tlbexp utility is shown below:

D:\Temp\RegAsmExample>tlbexp MathsFunctions.dll
Microsoft (R) .NET Framework Assembly to Type Library Converter 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Assembly exported to D:\Temp\RegAsmExample\MathsFunctions.tlb

Runtime Callable Wrapper (RCW)

Conversely, a COM component can be used within a .NET program by using a feature called Runtime Callable Wrappers.  In order to use legacy COM components from within .NET, you use the tlbimp utility, which imports a type library, and exports metadata for the .NET runtime.  An example of the usage is shown below:

D:\Temp\TlbImpExample>tlbimp reikanutils.dll /out:ReikanUtilsLib.dll
Microsoft (R) .NET Framework Type Library to Assembly Converter 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Type library imported to D:\Temp\TlbImpExample\ReikanUtilsLib.dll

RCW Example - using a COM component in .NET

This shows a step-by-step example of how to use a COM component from within a .NET program.  You can download the complete example (VB and C# code and output) from the link at the top of the page (TlbImpEx.zip).

Creating the COM DLL

First, we create a very simple COM DLL from Visual Basic.  The project is an ActiveX DLL, created in Visual Basic 6, with the project name as ReikanUtils, and the Class Module name is Maths.  The following code is within the Class module:

Option Explicit

Public Function Square(ByVal a As Double) As Double
  Square = a * a
End Function

Public Function SquareRoot(ByVal a As Double) As Double
  SquareRoot = Sqr(a)
End Function

 So, we have 2 very simple methods - one calculating the square of a number, and one calculating the square root of a number.

Build the COM DLL

You now need to build the DLL from Visual Basic (File | Make...).  The resulting output file (ReikanUtils.DLL) is a COM DLL which contains these two functions.

Generate the .NET Assembly containing the appropriate Metadata to describe the COM object

In order to be able to use the COM component from .NET, you need to generate a .NET assembly which contains the necessary metadata for defining the functions.  To do this, you just run the tlbimp utility:

D:\Temp\TlbImpExample>tlbimp reikanutils.dll /out:ReikanUtilsLib.dll
Microsoft (R) .NET Framework Type Library to Assembly Converter 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Type library imported to D:\Temp\TlbImpExample\ReikanUtilsLib.dll

Notice that the command line, in this instance, takes 2 parameters - the first if the name of the COM DLL, and the second in the name of the .NET DLL which will contain the necessasy metadata.

Write a Client Program

Now, we write a short "Client" program to use the functions:

using System;
using ReikanUtilsLib;

public class TlbImpExample
{
  public static void Main()
  {
    MathsClass Maths = new MathsClass();

    Console.WriteLine("The Square of 5 is " + Maths.Square(5));
    Console.WriteLine("The Square Root of 121 is " + Maths.SquareRoot(121));
  }
}

There's nothing too special about this - it's just a standard C# program.  We import the System namespace so we don't have to fully qualify the Console object, and we also import the ReikanUtilsLib namespace.  This is the namespace of the new .NET assembly DLL which contains our metadata, as created by the tlbimp utility.  We instantiate the COM object which is defined as MathsClass within the assembly.  The client just displays the square and square root of 2 numbers.

Inspect the .NET Assembly (Optional)

Note that you can use the ildasm utility (IL Disassembler) to extract the metadata to determine the names of the classes and namespaces.  The sort of information you can get from the ildasm utility is shown below:

Compile and Run the Client Program

Finally, we need to compile the C# program.  In order for the compiler to know that we want to use the ReikanUtilsLib.dll assembly, we need to explicitly state this in the command line (otherwise you'll get an error when compiling).  To do this, use the /r: switch as shown:

D:\Temp\TlbImpExample>csc /r:ReikanUtilsLib.dll TlbImpExample.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

And the result of running the program:

D:\Temp\TlbImpExample>tlbimpexample
The Square of 5 is 25
The Square Root of 121 is 11

 

Page Last Updated on Wednesday September 04, 2002