ARTICLE

Accessing .NET Components using VB.NET

Posted by Imtiaz Alam Articles | Visual Basic 2010 May 21, 2004
This article explains step by step of accessing .net components using VB. The code is compiled using beta2. Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]. It can be used with Beta1 with some minor modification.
 
Reader Level:

SUMMARY :

This article explains step by step of accessing .net components using VB. The code is compiled using beta2. Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]. It can be used with Beta1 with some minor modification.

This week we will see how we can access .net components with classic COM based client. We are aware, that .net component does not uses registry nor it has GUID, ClassIDs, IUnknown interface etc. like we use to do in our classic COM components. Thanks to Microsoft for removing complexity and making everything very very simple. Let's Start...

Tools Required:

RegAsm.exe: (Assembly Registration Tool) this tools can be used to register or unregister a .NET class with COM. It adds information about the .net type  to the system registry so COM clients can use the .NET class transparently.

GACUtil.exe : (Global Assembly Cache) we will use this tool, whenever we want to make assembly shareable so that it can be used by multiple application or if we want to remove it from global sharing.

SN.exe : (Strong Name) this tool is used to generate strong name for our assembly, which will make our assembly globally unique.

There will be two scenarios, one when we want to write component for only one client and other when we want to write component for multiple clients.

Writing for Single CLient:

We will first see the example of writing component which will be used by only one client. Let us begin with creating a simple C# class, which will have one method, which will take two no. as input and return the sum.

//Written By Imtiaz Alam
//clsAddPrivate.cs
using System;
namespace Imtiaz
{
public class AddTwoNumbers
{
public long Add(int x, int y)
{
return (x + y); // Return the Sum
}
}
}

As usual let us compile this using /target:library switch...

D:\Imtiaz\Projects\C#\Components\Add>csc /target:library clsAddPrivate.cs

Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.

Since we will be using our component with classic COM client, we have to fool our COM client by generating the GUID and making entry into registry andto make it think that it is using the classic COM component, and we also need to generate the Type library. For this we will be using  RegAsm.exe.

D:\Imtiaz\Projects\C#\Components\Add>RegAsm /tlb:clsAddPrivate.tlb clsAddPrivate.dll
RegAsm - .NET Assembly Registration Utility Version 1.0.2914.16
Copyright (C) Microsoft Corp. 2001.  All rights reserved.

Types registered successfully
Assembly exported to 'D:\Imtiaz\Projects\C#\Components\Add\clsAddPrivate.tlb', and the type library was registered successfully

We can see from the message that it generated the type library and also regsitered it into the registry. We could have also used TlbExp.Exe to generate type library. The advantage of RegAsm.Exe is that, it allows both registering and generating Type Library. Let us verify it by looking into the directory...

D:\Imtiaz\Projects\C#\Components\Add>dir
 Volume in drive D is DOWNLOADS
 Volume Serial Number is 355C-1B0A

 Directory of D:\Imtiaz\Projects\C#\Components\Add

10/28/2001  02:43p      <DIR>          .
10/28/2001  02:43p      <DIR>          ..
10/28/2001  02:40p                 229 clsAddPrivate.cs
10/28/2001  02:43p               3,072 clsAddPrivate.dll
10/28/2001  02:44p               1,540 clsAddPrivate.tlb
               3 File(s)          4,841 bytes
               2 Dir(s)   1,178,304,512 bytes free


We can use Oleviewer  to view the type library (clsAdd.tlb) generated by RegAsm.Exe

We verified the Type Library is generated and registered. Now let us write a VB application to access the .net component.

NOTE: We need to copy our component into the same directory where the client application will run.

Here is the simple VB code, we have  to reference the type library first.

Private Sub Command1_Click()
Dim objAdd As New clsAddPrivate.AddTwoNumbers
MsgBox(objAdd.Add(10, 20))
End Sub

Press F5, we all guessed the output (30), right?. If for some reason it does not works and gives the following error message,


It indicates that the .net component is not in the same directory, from where we are running the VB application.

Writing for Multiple CLient (Shared Component):

Now we will look the way we can register our .net component into Global Assembly Cache, so that we can access from multiple client.

To make our assembly (clsAdd.Dll) commonly available we have to generate a shared name (or Strong Name), which will make our assembly uniquely identifiable using Sn.Exe. A strong name is made up of the full class name including the namespace, the version number, culture information (which can be null if culture neutral), plus a public key and a digital signature.

To generate strong key for our component use the following Syntax; i.e.

D:\Imtiaz\Projects\C#\Components\Add> sn -k clsAddPublic.snk

Microsoft (R) .NET Framework Strong Name Utility  Version 1.0.2914.16
Copyright (C) Microsoft Corp. 1998-2001. All rights reserved.

Key pair written to clsAdd.snk
Now we need to add the .snk file in our code

//Written By Imtiaz Alam
//clsAddPublic.cs
using System;
using System.Reflection;
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyKeyFile("clsAddPublic.snk")]
namespace Imtiaz
{
public class AddTwoNumbers
{
public long Add(int x, int y)
{
return (x + y); // Return the Sum
}
}
}

Now let us compile the new code...

D:\Imtiaz\Projects\C#\Components\Add>csc /target:library clsAddPublic.cs

Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.

Registering into Global Assembly Cache:

Now we have generated the shared name, included in the code, compiled it and only thing left is to register it globally, so that it can be accessed by any application, using gacutil.exe

D:\Imtiaz\Projects\C#\Components\Add>gacutil /i clsAddPublic.dll

Microsoft (R) .NET Global Assembly Cache Utility. Version 1.0.2914.16
Copyright (C) Microsoft Corp. 1998-2001. All rights reserved. Assembly successfully added to the cache.

Let us look at the Global Assembly Cache

Now we will register it and write VB client to access it..

D:\Imtiaz\Projects\C#\Components\Add>RegAsm /tlb:clsAddPublic.tlb clsAddPublic.dll
RegAsm - .NET Assembly Registration Utility Version 1.0.2914.16
Copyright (C) Microsoft Corp. 2001.  All rights reserved.

Types registered successfully
Assembly exported to 'D:\Imtiaz\Projects\C#\Components\Add\clsAddPublic.tlb', and the type library was registered successfully

Set reference to clsAddPublic

Private Sub Command1_Click()
Dim objAdd As New clsAddPublic.AddTwoNumbers
MsgBox(objAdd.Add(10, 20))
End Sub

Conclusion:

There are two ways of accessing .net component using any COM client. If the component is private, we have to copy .net component into the client executable folder. And if we want to access our component from multiple application, we can move it to Global Assembly Cache. As usual to keep everything simple and to focus on the purpose of the article no error handling has been done.

share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor