You may be aware of the “Interop Toolkit” for Visual Basic 6 and .NET that allows you to call .NET code from visual basic 6. This is a nice feature but If you are trying to migrate out of VB6 the toolkit more or less forces you to keep your main application window to be a VB6 application. Many people have asked Microsoft to make the tool kit work both ways but to date Microsoft has ignored these pleas. The vbGuru shares your pain, he has been working with a company that is migrating 20 years of VB6 code over to .NET while new requirements are constantly being added. All with a very small team. (I think they’ve been working on it for over 10 years now, but are still stuck with lot of VB6 code.)
I have figured out a way for them to use an Interop approach that allows the main application to be VB.NET and uses an Interop style to call VB6 code (including forms) whenever legacy code has not yet been replaced. And it is actually very easy for anyone to do in about 5 easy steps:
1) Convert your VB6 EXE project into an ActiveX EXE project.
2) Add a public class library and expose everything you need from your application.
3) Create a new project (can not be in the same project group) that is an Active X DLL.
4) In the Active X DLL Project, Reference the ActiveX EXE.
5) In your .NET project Reference the Active X DLL.
You can’t reference an Active X EXE in .NET, which would make this even simpler, but you can reference it from an ActiveX DLL.
Inside the ActiveX EXE I created a class Named “LegacyForms.cls”
Inside of it I created a public method for every form in my existing project:
Public Sub ShowfrmMain()
frmMain.Show
End Sub
Inside the ActiveX DLL I created a Public Class Called “Legacy.cls” which had a public method like so:
Public Sub ShowfrmMain()
Dim fLeagcy As New Legacy.LegacyForms
fLeagcy.ShowfrmMain
End Sub
Now form my VB.NET code I can call:
Legacy.ShowfrmMain
It’s very easy to pass information to the VB6 code by just adding parameters to the public methods, sending data back to VB.NET is a little more tricky, but it can be achieved with public properties, methods, or if you want to be super fancy you could raise an event back to VB.NET.
Hope that makes sense.