Thursday, December 01, 2005

Debugging VB 6.0 Code From Visual Studio 2003

It is not uncommon to need to call a VB 6.0 ActiveX DLL from a .NET application, especially if you are doing new development for an existing software system. The .NET framework has some nice features that makes calling VB 6.0 code from .NET code fairly painless. One thing that is a little confusing, however, is how to debug VB 6.0 code if it is called from a .NET application. This article will hopefully shed some light on this mystery.

Building Your VB 6.0 Code
In order for Visual Studio 2003 to be able to debug VB 6.0 code, it needs to have a Program Debug Database file (sometimes called a debug symbol file) for the code. Unfortunately Visual Basic 6.0 does not create this by default, but it is easy to get VB to do this. Using VB 6.0 open the project you want to be able to debug in Visual Studio 2003. Go to the Project Properties dialog and select the Compile tab.



Check the Create Symbolic Debug Info checkbox and press OK. When the project is rebuilt, a Program Debug Database file with a pdb extension will be generated. This is the file Visual Studio 2003 will use to debug the VB 6.0 code.

Enabling Unmanaged Code Debugging
The next step is to enable unmanaged code debugging (sometimes called native code debugging) in your Visual Studio 2003 project. In the project properties form for your Visual Studio 2003 project, select the Debugging page and check the “Unmanaged code debugging” checkbox. This allows Visual Studio 2003 to debug VB 6.0 code (as long as a Program Debug Database file is available).



Enabling this option really slows down the debugger. Depending on your situation, it may be wise to create two different debug configurations, one for debugging managed (.NET) code only and one for debugging both managed and unmanaged (VB 6.0) code. I typically call the configuration for debugging both managed and unmanaged code “Native Debug”. You can create this new configuration using the Configuration Manager. To get to the Configuration Manager click the Configuration Manager button. When the Configuration Manager is displayed, select in the Active Solutions Configuration dropdown to create this new configuration. Be sure to select the existing Debug configuration on the Copy Setting from dropdown list when you create your Native Debug configuration.

Setting a Breakpoint
Once the Unmanaged code debugging option has been selected you can set breakpoints in the VB 6.0 code. To set a breakpoint in VB 6.0 code, you will have to manually open the source file where you want to place the breakpoint. Select File -> Open -> File… from the Visual Studio 2003 menus and browse to the source file you want to set a breakpoint in. You can create a breakpoint in this source file the same way you create breakpoints in .NET managed code.

Stepping Around
If the application you are debugging uses early binding to create your VB 6.0 object, you can use command such as Step Into, Step Out, etc. to go between managed .NET code and unmanaged VB 6.0 code. If the application uses late binding you will not be able to use Step Into and similar command to go from managed .NET code to unmanaged VB 6.0 code. In these situations you will have to set a breakpoint in the unmanaged VB 6.0 code.

Debugging a Running Process
To debug a program that contains both managed .NET code and unmanaged VB 6.0 code that is already running select Tools -> Debug Processes… from the Visual Studio 2003 menu. Select your process and press the Attach… button. When the Attach to Process dialog is displayed, be sure that both Common Language Runtime and Native options are selected.



Once you are back in the Visual Studio, you can create breakpoints and step through both your managed and unmanaged code.

No comments: