Home » Articles | Snippets

Ghosts from the past - How to reference a .NET assembly in VB6

8. September 2010 by Juliën Hanssens 0 Comments

Ghost_of_Christmas_PastEvery once in a while every modestly “seasoned” developer gets a visit from a ghost from the past.  With any luck the visit is limited to sins committed by the developer in the more recent past, but sometimes them old wounds will be opened up.
In this specific case a request was made to add some new functionality to a VB6 solution. And I think everyone will agree on the fact that VB6 is more than a ghost from the past: it can be considered a ghost in a haunted house according to Wikipedia:

A place where ghosts are VB6 is reported is described as haunted, and often seen as being inhabited by spirits of deceased who may have been former residents or were familiar with the property. Supernatural activity inside homes is said to be mainly associated with violent or tragic events in the building's application’s past such as murder, accidental death, or suicide — sometimes in the recent or ancient past.

That sure summons up the usage of VB6 in 2010 for you! But being cynical on the matter doesn’t help solve the problem. Instead, on a creative burst we decided on a more elegant approach. Like creating the functionality in a standalone .NET assembly (C# class library), which we then would reference in VB6. Considering VB6 knows something about COM we would put our horses on that solution and prayed we would get this working.

Luckily, if you break this down into some small chucks it’s actually doable and not too much work to cope with. There are three steps:
1.) create and prepare the .NET solution
2.) convert the DLL to TLB and register it on the target machine and
3.) import it in VB6.

Let me demonstrate.

 

Step 1 - Prepare the .NET assembly

 

  1. Start up Visual Studio and create your Class Library project
  2. In the Project Properties, select the Build tab and enable Register for COM interop
    RegisterForInterop
  3. Create a class and decorate it with the following attributes: Guid, ClassInterface and ComVisible.
    Also note the ComVisible attribute on the Ping() method:
    [Guid("0EE739C1-E194-4DB5-AB2A-ED027AD07A96")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class Interop
    {
        [ComVisible(true)]
        public string Ping()
        {
            return "Pong!";
        }
    }
  4. In order to use the Assembly Registration Tool in the steps below, we need to sign the assembly with a strong name by:
    StrongSignAssembly
    1.) Select the Project Properties > Signing tab
    2.) Enable the Sign the assembly checkbox and select a <New…> file
    3.) Enter a name (by convention you could use the same name as your assembly). A password is not required for this exercise.
  5. Build it.

That’s it for the .NET part. Now it’s time to choose your path wisely: if you build the .NET solution on the same machine as the VB6 project , there is a chance that you can skip some steps. We call this path 2a. Otherwise, of if you’re not sure, just always take the blue pill follow step 2b.

 

Step 2a – VB6 running on the same system

If you are running VB6 on the same system at which you built the .NET assembly from the first step, you’re in luck! It will save you some keystrokes. And I know how developers loooove to save keystrokes.

 

  1. Start the Visual Studio Command Prompt and navigate to your .NET project’s \bin directory.
  2. Run the Assembly Registration Tool to create a compatible interop file using the following command:
         RegAsm YourAssemblyName.dll /tlb

With this, proceed to step 3.

 

Step 2b – VB6 running on a different system

If you are running VB6 on a different machine than your .NET environment, you first have to copy the

  1. Copy the output of the \bin directory to your VB6 machine
  2. Start the Visual Studio Command Prompt and navigate to the directory mentioned above
  3. Optional – in some cases you have to manually create a Codebase entry in the registry. So, just do it:
         RegAsm YourAssemblyName.dll /codebase
  4. Run the Assembly Registration Tool again to create a compatible interop file:
         RegAsm YourAssemblyName.dll /tlb

Step 3 – Reference the assembly in VB6

At last we get where we want to. We have prepared all the ingredients and are ready to start cooking it up into a nice VB6 stew.

  1. Start your VB6 development environment (notice the speed?) and create a new VB6 project
  2. Select Project > References and add a reference to the created .TLB file.
  3. Try to call it in a manner like such:

Dim myObj as YourAssemblyName.Interop
Set myObj = new YourAssemblyName.Interop
 
Dim strTest as string
strTest = myObj.Ping()
MsgBox strTest
Now with all these steps in place you have a likely chance to be able to F5 it and see have it all working. With a large emphasis on “likely”.

Comments are closed