This article explains the step by step procedure of how to use a .NET Assembly with COM Client.
Step 1: Creating a .NET Assembly Project
1.1 Visual Studio .NET IDE -> File -> New Project ->
Visual Basic Projects -> Class Library
1.2 Name the Application, for instance NetServer, this will
be our .NET Assembly Project,which will be consisting of at
least one class called Class1.vb.
1.3 Rename the class to NetClass from properties window and manually
in code window.This class will hold all the functionality of .NET Component,
in the form of few functions.
Step 2: Adding Functionality
On the path of achieving COM Interoperability, Microsoft .NET
offers an attribute named <COMCLASS()>,which is
located inside the Micrsoft.VisualBasic namespace
and it makes the .NET Class available for use by a COM Client.
It’s also a wise choice to add System.Runtime.InteropService namespace to the class, which offers various features to be used.
The figure below shows the whole Code of .NET Assembly.
Step 3: Set Property for COM Interoperability
Select NETServer project from Solution Explorer, Right Click ->
Properties -> Configuration Properties -> Build -> Check on
Register for COM Interop
After setting the property, do Build The Solution. It will create
a NetServer.dll (assembly) in your applications \bin folder.
Step 4: Deploying for COM access
A .NET assembly which has been created can’t be used by a COM Client,
because COM Client can access an object with help of a Type Library,
which tells to the Client about the Object.
In .NET world there is no such concept of Type LIbrary, but because of
COM Interop feature, .NET Framework SDK offers a tool called
RegAsm.Exe which offers to make a type library and
register it in Windows Registry, the very own style of COM.
4.1 Access Command Prompt (cmd)
4.2 Change the path to your application folder
for example : C:\>D: press enter
D:\> cd Net Applications\NetServer\Bin
4.3 Type the following command to create a
Type Library,which is a COM world’s buzz word and equivalent
to Metadata of a .NET Assembly.
D:\Net Applications\NetServer\Bin> RegAsm /tlb: NetServer.tlb NetServer.dll
This command will create a Type Library named NetServer.tlb
in the \bin folder of .NET Assembly application. Which was
automatically registered in Windows Registry as well.
Now the .NET Assembly is ready to use by a COM Client.
Step 5: VB 6.0 Client to access .NET Assembly
5.1 Open Visual Studio 6.0 -> Visual Basic 6.0 ->
File -> New -> Standard.Exe
5.2 Drag one Label and two Command Buttons onto the form.
Step 6: Set Reference to the Type Library
Before consuming the class you build using .NET
Project -> References -> Find the NetServer and select that.
Step 7: Code to access the .NET Class
after adding the code, run your VB 6.0 and click on the command buttons, you will see that your application communicating with .NET Assembly.