Monday, February 4, 2008

Steps to make your .Net DLL useable from VB6

In order to use a .Net DLL from VB6 I had to take the steps below. This assumes that you have already created a .Net dll with a public method. Before you begin, in VS2005, build your project and then go into the "Project-References" box in your VB6 application. At this stage you will not be able to see your .Net DLL.

Step 1. Make it COM-visible.

Right click on the project In VS2005 and select "Properties". Under the "Application Tab" click the "Make assembly COM-Visible" checkbox. Then click on the "Build" tab and check the "Register for COM interop" checkbox in the "Output" section. Save.

If you try and build the project now you will find that you can reference the project in VB6, and can instantiate it in code, but you cannot view the methods in the Object Browser or get VB6 IntelliSense in the methods. You can call the method and get your result back though.

Step 2. Implement an interface.

Your class should implement an interface. Convert your class from this:

    public class MyClass
{

public String testInOut(String sIn)
{
return ret = "Returning [" + sIn + "] at " + System.DateTime.Now.ToString();
}
}


to this:

    public interface IMyClass
{
String testInOut(String sIn);
}

public class MyClass : IMyClass
{

public String testInOut(String sIn)
{
return ret = "Returning [" + sIn + "] at " + System.DateTime.Now.ToString();
}
}


Or, even easier, select the class and method definition and right-lick, choosing "Refactor-Extract Interface..." and check the method in the box that pops up. This will generate an interface into a new file and modify your class to implement it.

Step 3. Expose your methods.

Get .Net to expose your methods to VB6. Do this by adding the [ClassInterface(ClassInterfaceType.AutoDual)] attribute to the public class, and importing the InteropServices library:

...
using System.Runtime.InteropServices;

namespace MyNamespace
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyClass : MyProject.IMyClass
{


If you rebuild your .Net dll now, you should be able to see the methods in the VB6 Object Browser. Now we need to make sure we can deploy it remotely. Follow these additional steps.

Step 4. Control your GUIDs.

Add GUIDs to both the Interface and the public class. To get a GUID, click "Tools-Create GUID" and choose option 4 "Registry Format". Click "New GUID" and then "Copy". Paste as a "Guid" attribute into your interface, stripping out the curly brackets. Then generate a new GUID and paste an attribute into your public class. The GUID against the public class is the key one because it is the one that is looked up in the registry to determine which DLL your program will use. Specifying the GUIDs in these attributes ensure that the same GUID is used when you compile your build.

    [Guid("3A7E8E37-3B6B-4cda-9A47-EBD0D1D11812")]
interface IMyClass


and

    [ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("87E9EBBD-CE79-4336-BB7F-F070483C442C")]
public class MyClass : MyProject.IMyClass


Step 5. Sign the assembly with a strong name.

Go into the "Project-Properties-Signing" tab and select "Sign Assembly" and choose the "string name" combo entry.

Now we are read to deploy this remotely. Follow the steps below on the remote machine:

Step 6. Install into the GAC.

Copy the file to the remote machine (which already has the .Net framework installed) into some directory {myfolder}. Then copy it into the c:\windows\assembly folder. This is the location of the GAC (Global Assembly Cache). .Net applications on this machine will now be able to use it. However VB6 will still not be able to find it.

Step 7. Register the DLL.

Register the DLL with COM via the regasm tool:

    regasm c:\{myfolder}\MyProject.dll


The regasm tool is installed as part of the .Net framework and can be found here:

    C:\WINDOWS\Microsoft.NET\Framework\v2.0.5072


This will register the type library. If you try to use the COM object via VB6 it will pick up the version from the GAC. The version you have copied to {myfolder} can be deleted if required. And that is it!

1 comment:

bvdommelen said...

Step 6 didn't work for me; I used "GACUTIL /i my.dll" at the command line and that did the trick