Build a .NET Server
Callable from COM Clients
This article
elucidates how to build and install-managed code that will be used from COM
applications. A classic COM server is activated using the Service Control
Manager (SCM). It looks up numerous information such as CLSIDs, IIDs, ProgIDs
etc.
So what is the
solution to use the .NET assemblies in the classic COM clients,
These are the steps
concerned in the build process are as follows:
-
Write and compile
the managed code.
-
Generate a COM type library
(*.tlb) for the assembly using the tlbexp.exe utility so that allow the COM
client to interact with the exposed types.
-
Install and register the
assembly so that COM SCM to locate it.
-
Write and compile
the COM code that references types in the assembly.
For demonstration
purposes, I have created a .NET component in C# named Calculator. For client
side, I have created a Visual Basic (VB) 6.0-based client.
Writing and Compiling the Managed Code:
To illustrate COM type
communication with managed code, Let us see an example in which I create a C#
class library which has a class named Calculatorwhich supports three methods
named Add() ,Subtract()and Hello(). Notice that we define another interface
named Imuldiv.
namespace Simpleclasslib
{
using System;
using System.Runtime.InteropServices;
public interface Imuldiv
{
int Multiply(int x,int y);
int Division(int x,int y);
}
public class Calculator:Imuldiv
{
public Calculator(){}
public int Add(int x,int y)
{
return x+y;
}
public int Subtract(int x,int y)
{
return x-y;
}
int Imuldiv.Multiply(int x,int y)
{
return x*y;
}
int Imuldiv.Division(int x,int y)
{
return x/y;
}
public string Hello(string strName)
{
string str
;
str = "Hello " + strName ;
return str
;
}
}
}
Once the managed code is written, the compilation process is the same as it
would be for any other piece of managed code.
csc /out:Server.dll /target:library Calculator.cs
Now let us see the Server.dll in the ILDasm.exe.In that you can see the
calculator class members and Imuldiv Interface members.
GENERATING A TYPE LIBRARY AND REGISTER THE
ASSEMBLY:
After compiling the project we have to create a type library file. So that only
most unmanaged application development tools require a type library before you
can make references to external types.
COM states that all server application that is programmed to share their
features with other applications is to be register at a common location. The
client needs to determine the exposed methods, properties and events. This is
done via the libraries. The client reads the registry; determine the properties,
methods, and event s of the object. COM requires up information such as CLSIDs,
IIDs, and ProgIDs etc. But we know the assemblies are not registered.
.NET framework does not depend on the registry and uses metadata for this
information. Hence, we have to produce the COM-compatible registry entries for
our managed server so that the COM runtime could instantiate our server.
The .NET framework provides a couple of tools for this. You can use the Type
Library Exporter utility (TLBEXP.exe) or the Assembly Registration Utility
(Regasm.exe), both of which you'll find in the Bin directory of your .NET SDK
installation.
REGASM is a superset of the TLBEXP utility in that it also does much more than
generating a type library. It's also used to register the assembly, so that the
appropriate registry entries are made to smooth the progress of the COM runtime
and the .NET runtime to fastener up the COM aware client to the .NET component.
Usually a type library can be generated from an assembly using the regasm.exe
utility. The regasm.exe utility not only registers an assembly and it also
creates the required type library file, as shown here.
regasm Server.dll /tlb:Netserver.tlb
Now let us see the NetServer.tlb in the OLE viewer.
After creating the type library file you have to add a reference this to your
project. For example, with Visual Basic 6.0, you can reference the .tlb file or
dll file from the Project/References dialog. In Visual C++ 6.0, you can use the
#import statement to import the type definitions from the type library directly
into C++. Once the reference to the type library is added to the project, the
types defined within that library can be referenced from unmanaged code.
INSTALLING THE ASSEMBLY:
In order to actually create managed types from unmanaged code, the assembly
needs to be installed in the global assembly cache (GAC) and registered for use
from COM.
You can install an assembly in the global assembly cache using gacutil.exe
utility. Assemblies can be uninstalled using the /u option.
gacutil /i simpleserver.dll
Writing and Compiling the Unmanaged Code:
Once the assembly is registered and properly installed, the types defined within
the assembly can be used from COM as though they were normal COM types.
Now you can create a simple VB 6 Standard EXE project type and as I already said
confirm whether you set a reference to the new generated type Library.
Place this code in the code section and now you see the usage of our .NET type
in Classic COM.
Private Sub Command1_Click()
Dim a As New Calculator
MsgBox(a.Add(100, 100), vbInformation, "ADDITION (100,100)")
MsgBox(a.GetType, vbInformation, "INFORMATION-TYPE")
MsgBox(a.Hello("India"), vbInformation, "STRING")
MsgBox(a.Subtract(100, 1), vbInformation, "SUBTRACTION(100-1)")
Dim i As Imuldiv 'INTERFACE
i = a
MsgBox(i.Multiply(100, 100), vbInformation, "MULTIPLICATION(100*100)")
MsgBox(i.Division(100, 100), vbInformation, "DIVISION(100/100)")
End Sub
Summary:
A lot
of companies have spent a great amount of money and time in COM components. With
introduction of .NET people are worried about the future of COM. Microsoft has
recognized this and provide means to use classic COM components in .NET code and
vice versa.