(Print this page)

Using VB6 forms in a .Net application
Published date: Wednesday, May 22, 2013
On: Moer and Éric Moreau's web site

I am always surprised to find out how many VB6 applications are still running out there running after more than a decade of its official death. That doesn’t mean that these companies haven’t started their migration to .Net, it only means that many important assets are still running in VB6. In many situations, users are obliged to use 2 applications (one in .Net and one in VB6) to fulfill their tasks which is far from ideal for them.

With some little modifications, your VB6 can probably be reused in your .Net applications so that your solution seems a bit more integrated from the user’s perspective.

The Microsoft Interop Toolkit confusion

Microsoft released a long time ago (2007 if not mistaken), the Microsoft Interop Toolkit. The current version is now 2.1 (released in April 2010). This toolkit allows you to host .Net forms in VB6 applications. But this toolkit is exactly the opposite of what I want to show you (and needed for my next project).

What you need?

You will need to have your VB6 forms in an ActiveX DLL project. Currently, your forms are probably directly in your application .EXE file so that means that you will have to move your forms to another project type and recompile it.

You will also need some code that Eduardo Campano published on Code Project in July 2007. I strongly encourage you to read is article to find out the details of the code. The interesting parts of his application will also be available here.

The downloadable demo

This month code is provided in both VB and C#. It has been created with Visual Studio 2012 but can surely be reused with any other version of Visual Studio .Net since we are not referring any special components.

The VB6 project

I haven’t modified Eduardo’s code much. I have only fixed a couple of typos here and there and I have added a textbox on that same form just to test the copy-and-paste feature which works just as it should be. I have also made sure that the VB6 form instance is a single instance (to prevent creating new instances).

The very first thing you will need to ensure is that your VB6 forms are compiled in a ActiveX DLL project as shown in figure 1.

Figure 1: The VB6 project’s properties

The reason is that .Net cannot reference the application .EXE.

The second thing you will is to steal the ClsForms file from Eduardo’s VB6 project because it contains a method named OpenForm which will be used by the .Net application to create an instance of a VB6 form in that ActiveX DLL. Some other methods are also used to get the VB6 forms look a little more .Net-ish.

Very important: VB6 is 32-bits only

If there is one thing that might prevent you from using this technic is that all VB6 compiled .DLL (and even .EXE) are 32-bits only and your .Net application might not.

If you create your host application in VB.Net, it doesn’t seem to make a huge difference. I have tried both x86 and x64 and they were both working exactly the same way.

But if your host application is in C#, be sure you are targeting the x86 platform as shown in figure 2 (otherwise, you won’t be able to create an instance of your VB6 DLL and your application will crash).

Figure 2: C# project properties

C# early/late binding

Visual Basic as always been known for its ease of handling late binding. Late binding is nothing more than resolving at runtime the different members (mainly properties and methods) instead of doing it at compile time. It is easier than done!

In this article, and the way Eduardo created his sample, late-binding is fully used, at least for the VB.Net project. You will never find a reference to the VB6 component in the VB.Net project. Because the compiler is not aware of the component, of course the Intellisense is not able to provide you methods, arguments and everything you are used to but at least it let you write names of methods that will hopefully be found at runtime. At the top of the frmContainer.vb file, you will find a directive that will specifically allow late binding:

Option Strict Off

If you turn this option on, you will all of the sudden discover multiple compilation errors. So keep it off and enjoy the late binding power.

On the other hand, C# is not really “late binding friendly”. Of course you could use reflection but that would have changed the original code radically and I didn’t want to go that way. Instead, I have opted for the easy solution. I have added a reference to the FormsVB6.dll COM component to the C# project. This let me create an instance of a specific type and the compiler (and Intellisense) can fully do its job of resolving methods name and so on.

Of course you can add a reference to the COM component to the VB.Net project. If you do so, change the data type from Object to the specific type as it is done in the C# version.

Deployment

Don’t forget that because you are deploying a COM component, you will need to distribute the VB6 run time, as well as your DLL (and any of its referenced components) and finally register the VB6 DLL. This might be a pain!

If you need to reinstall VB6

VB6 hasn’t been installed on my main OS for years. The very few times I needed it, I used a virtual machine that I still have for emergencies. But for fun and to ease the writing of this article, I tried to reinstall VB6 on my main OS which is Windows 8 64-bits. If you try to install it, chances are that your installation will not complete successfully. It will probably run “not responding” without ever completing even if you set the compatibility to XP and run as administrator.

I found my solution at http://vb6andwindows8.codeplex.com/. This issue is caused by some components that are not compatible anymore. You need to unselect the “Data Access” components from the installation options.

I also strongly suggest that you install SP6 for VB6.

Conclusion

You may still not have completely converted your legacy VB6 applications to .Net but that doesn’t mean that you need your users to connect to 2 applications (or more). It is relatively easy to use VB6 form in your .Net application.


(Print this page)