Debugging Hybrid VB6/.NET Applications

To wrap up this series of posts on hybrid applications with the Interop Forms Toolkit, I'd like to talk about debugging. There's a couple ways we can go about debugging with VB6 and Visual Studio and depending on where you're spending most of your coding time, you may opt for one method or the other.

The first way we can debug is by attaching to the running VB6.EXE process from Visual Studio. This works well if you are working primarily in VB6 code. You'll need to already have the VB6 IDE open and the project running in order to attach properly. Start your VB6 project from the VB6 IDE then go back to Visual Studio and select Tools --> Attach to Process and select VB6.EXE. It's important that the VB6 application is running and not in design mode when you attach.

Now you'll be able to set breakpoints in both Visual Studio and VB6 and have them step through each other by hitting F8. (NOTE: The Visual Basic Development Settings profile gives you the VB6 keyboard mappings, including F8 for stepping through code. If you just want F8 and not the other VB mappings, go to Tools --> Options --> Environment --> Keyboard and select the Debug.StepInto command.)

The other way to debug across both environments is to use the Debug Start Action. This method works well if you are primarily working in Visual Studio like when you are first creating your .NET controls/forms. This is because when you need to recompile your .NET assembly you'll sometimes need to close VB6 in order to see the new changes. This is because VB6 hangs onto the DLL while you're developing. It may be easier for you to set up debugging this way because VB6 is automatically started when you start the Debug from Visual Studio. But watch out, the VB6 IDE is also closed as well when you stop debugging in Visual Studio and so any changes that you make to your VB6 application (like via Edit and Continue) you'll need to remember to explicitly save by stopping the application in VB6 and saving the project.

To set this up, go to My Project --> Debug tab then select "Start External Program" and find your VB6.EXE (i.e. C:\Program Files\Microsoft Visual Studio6\VB98\VB6.EXE). Next you'll need to specify your VB6 project file and wrap the argument in quotes (i.e. "C:\VB6App\MyVB6App.vbp"). Finally you'll want to set the working directory appropriately. For instance, if you connect to a database file or other resources in the current directory then make sure you set that appropriately too.

Now when you start the Debug in Visual Studio, the VB6 IDE will open your project and you can then start the Debug there and set the necessary breakpoints.