Monday, July 13, 2009

Use your own VB.Net and C# Libraries in VB6


There are times when you need to use one of your .Net DLL’s in a Visual Basic 6 application.

Why should I bother, you ask? Let’s say that you want to retrieve information from a .Net Web Service from your VB6 app, it is easiest and fastest to code the call from a .Net Library than from VB6. Or you may have a Security Library that handles the user authentication and authorization of all the apps in your company, but you have some old VB6 programs that you can’t get rid of. In all these cases, it would be helpful to just invoke a method of your .Net Library from the VB6 code.

There is a series of requisites to make a .Net DLL usable from VB6.

1. Prepare your Class.

In your DLL, you should have a Main or Entry Point Class. That is the class that the VB6 app will call and use. It doesn’t matter what your other classes do, you just need to code the Main Class with these points in mind:

- Don’t use Shared or Static Functions. VB6 won’t be able to call these functions. As long as they are not been called from VB6, it doesn’t matter if you have Static functions in your code.

- Don’t user parameterized Constructors. To instantiate your class from VB6 there must be a default constructor (that doesn’t receive any parameters). Again, you can have all the constructors you want, but you will be able to use only the default from VB6.

- Add an Import (VB.Net) or Using (C#) to this namespace: System.Runtime.InteropServices


2. Prepare your DLL.

You have to change some settings to your project in order to make it usable from VB6:

- Sign the Assembly. Go to Project -> Properties and in the Signing Tab check the “Sign the Assembly” option. Then choose a strong name for your key file selecting “”, type a name and press OK (you don’t need to protect the key with a password)

- Make the Project COM Visible. Go to the Application tab in your project properties and click in “Assembly Information…” button. Check the option “Make assembly COM-Visible”. Now that you are here, fill the assembly information (product name, version, etc.) as this info will be displayed in VB6 when you reference to it.


- Make Intellisense work in VB6. You need to open the file AssemblyInfo.vb or AssemblyInfo.cs. You can open it from the Solution Explorer under the Properties folder. If you can’t see it, click the “Show All Files” button in the toolbar of the Solution Explorer. What you need is to add this line there (it could be anywhere in the file):

C#: [assembly: ClassInterface(ClassInterfaceType. AutoDual)]
VB.Net:


(If you have problems, you may come back here and change AutoDual to AutoDispatch)

- Add the path of the key.snk. In the AssemblyInfo file, add the path of the key you created previously:

C#: [assembly: AssemblyKeyFile(@"C:\...\MyKey.snk")]
VB.Net:


3. Prepare Client Machine.

When you build your project, you will notice Visual Studio generated two files: mydll.DLL and my dll.TLB. The second file is a description of the DLL used by VB6. In order to use the DLL from VB6 you need to register the assembly:

- First open a command prompt (Start -> Run -> “cmd”) and go to the .Net Framework directory:

> cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

- Now use the regasm command to register both DLL and TLB:

> regasm C:\[path]\MyDll.dll /tlb:MyDll.tlb

(If you need to uninstall the the .Net reference, use "regasm /u")

You will need to do this for your Development Machine and for the Client Machine. You can create an installer or a batch program to register the DLL and TLB in the client machine.

The last step is simple: open Visual Basic 6 Environment, go to Project -> References… and browse for your DLL.

Congratulations! Now you can instantiate your .Net Class from Visual Basic 6.

No comments:

Post a Comment