This documentation is archived and is not being maintained.

Debugging Hybrid Visual Basic 6.0/Visual Basic .NET Applications

Visual Studio 2005
 

Scott Swigart
Swigart Consulting LLC

July 2006

Applies to:
   Microsoft Visual Basic 6.0
   Microsoft Visual Basic 2005
   Microsoft Visual Studio 2005

Summary: This articles shows how to debug Visual Basic .NET applications and the Visual Basic 6.0 components that they use, and how to debug Visual Basic .NET components called from Visual Basic 6.0 applications. (8 printed pages)

Contents

Introduction
Debugging Visual Basic .NET Applications That Use Visual Basic 6.0 Components
Debugging Visual Basic 6.0 Applications That Use Visual Basic .NET Components
Conclusion

Introduction

As articles on the VB Fusion site show, it's not especially complicated to create applications that use both Visual Basic 6.0 and Visual Basic .NET. These may be applications that are primarily Visual Basic 6.0 but that call into the .NET Framework to access the extensive functionality that it provides. Alternatively, an application may be written in Visual Basic .NET but carry with it a number of "legacy" COM objects.

Like any application, these hybrid applications require maintenance and enhancements over time. Any time you crack those applications open, it's likely that you'll want to use the debugger to find and remove bugs in the code. For a Visual Basic 6.0 application, debugging is simple: you just set a breakpoint, and then you can step through the code. With a Visual Basic .NET application, it's equally simple: you again just set a break point and step through the code. But how do you debug hybrid applications? How do you debug when Visual Basic 6.0 code and Visual Basic .NET code call into each other?

Debugging Visual Basic .NET Applications That Use Visual Basic 6.0 Components

The following walkthrough will illustrate how you can debug Visual Basic .NET applications and the Visual Basic 6.0 components that they use.

  1. Open Visual Studio 2005.
  2. On the File menu, click New, and then click Project.
  3. Create a new Windows Application project named VBNET App.
  4. Place a Button control on the form.

    At this point, you have created a trivial Visual Basic .NET application that will soon call into a Visual Basic 6.0 COM object (Figure 1).

    Aa730840.dbugvb01(en-US,VS.80).gif

    Figure 1. Simple Visual Basic .NET application calling into a Visual Basic 6 COM object

  5. Start Visual Basic 6.0.
  6. Create a new ActiveX DLL project.
  7. Add the following code to Class1.
    Public Function HelloWorld() As String
        HelloWorld = "Hello World"
    End Function
    
  8. On the File menu, click Make Project1.dll.
  9. Click OK.

    This compiles the simple Visual Basic 6.0 COM object and creates the registry entries for it. At this point, you can just add a reference to the COM object from your Visual Basic .NET application.

  10. Switch to Visual Studio 2005.
  11. On the Project menu, click Add Reference.
  12. Click the COM tab.
  13. Select the Project1 component (Figure 2).

    Aa730840.dbugvb02(en-US,VS.80).gif

    Figure 2. Adding a reference to the Visual Basic 6.0 component

  14. Click OK.

    This is the component that you just created in Visual Basic 6.0. This COM component will be called from your Visual Basic .NET application.

  15. On the Form1 designer, double-click the button to generate the code for the click event handler.
  16. Code the Button1_Click event as follows.
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim c As New Project1.Class1
            MessageBox.Show(c.HelloWorld)
        End Sub
    
  17. Press F5 to run the application. When you click the button, you should see the Hello World message box (Figure 3).

    Aa730840.dbugvb03(en-US,VS.80).gif

    Figure 3. Visual Basic .NET application calling a Visual Basic 6.0 COM object

  18. Close the application.

    Now it's time to debug the Visual Basic .NET application and the Visual Basic 6.0 component that it calls.

  19. Switch to Visual Basic 6.0.
  20. Set a breakpoint on the following line of code.
    HelloWorld = "Hello World"
    
  21. Press F5.
  22. In the Project1 - Project Properties dialog box, click OK.
  23. If prompted to save your files, save them to a folder of your choice, or click Cancel to skip saving.

    This starts Visual Basic 6.0 debugging and loads the component.

  24. Switch to Visual Studio 2005.
  25. Set a breakpoint on the following line of code.
    Dim c As New Project1.Class1
    
  26. Press F5 to run the application.
  27. Click the button. Execution should stop at the break point in your Visual Basic .NET application.
  28. On the Debug menu, click Step Into.
  29. On the Debug menu, click Step Into again.
  30. Switch to Visual Basic 6.0. Execution should be stopped at your breakpoint (Figure 4).

    Using this technique, you can debug the Visual Basic .NET code and the Visual Basic 6.0 code. You are not actually stepping from Visual Basic .NET into Visual Basic 6.0; instead, by setting breakpoints in each, you can use the Visual Basic .NET and Visual Basic 6.0 debuggers simultaneously.

    Aa730840.dbugvb04(en-US,VS.80).gif

    Figure 4. Debugging a Visual Basic 6.0 COM component called from Visual Basic .NET

  31. Stop the Visual Basic 6.0 and Visual Basic .NET debuggers.
  32. Close Visual Basic 6.0 and Visual Studio 2005.

Debugging Visual Basic 6.0 Applications That Use Visual Basic .NET Components

You can also call into .NET components from Visual Basic 6.0 applications. This can be useful, because the .NET Framework contains thousands of useful classes, many of which have functionality (such as ping and FTP) that are not natively available in Visual Basic 6.0. The following walkthrough will show how you can debug Visual Basic .NET components called from Visual Basic 6.0 applications.

  1. Start Visual Studio 2005.
  2. On the File menu, click New, and then click Project.
  3. For the project template, select Class Library.
  4. Name the project VBNET Component, and then click OK.
  5. On the Project menu, click Add New Item.
  6. In the Add New Item dialog box, select COM Class and click Add.

    To make a .NET component that's callable from Visual Basic 6.0, the .NET component must present itself as a COM object. By using the COM Class template, you can create a .NET class that also presents a COM interface.

  7. Add the following code to ComClass1.
        Public Function HelloWorld() As String
            Return "Hello World"
        End Function
    
  8. On the Build menu, click Build Solution.

    This compiles the Visual Basic .NET project and registers ComClass1 as a COM object.

  9. Start Visual Basic 6.0.
  10. In the New Project dialog box, select Standard EXE and click Open.
  11. Add a Command button control to the form.
  12. On the Project menu, click References.
  13. Select VBNET_Component and click OK.
  14. On the Form1 designer, double-click the button to generate the event handler.
  15. Enter the following code.
        Private Sub Command1_Click()
            Dim c As VBNET_Component.ComClass1
            Set c = New VBNET_Component.ComClass1
            MsgBox c.HelloWorld
        End Sub
    
  16. Set a breakpoint on the following line of code.
        Set c = New VBNET_Component.ComClass1
    
  17. Switch to Visual Studio 2005.
  18. Set a breakpoint on the following line of code.
        Public Function HelloWorld() As String
    

    At this point, you're ready to run the application and use the debugger. However, debugging a Visual Studio 2005 component works differently than debugging a Visual Basic 6.0 component. Recall that when debugging the Visual Basic 6 component, you pressed F5 in the Visual Basic 6 development environment to "run" the debugger. This isn't how it's done in Visual Studio 2005. Instead, you attach the Visual Studio 2005 debugger to the process that will use the component. In this case, that process is actually the Visual Basic 6.0 IDE.

  19. Switch to Visual Basic 6.0.
  20. Press F5 to run the application.
  21. If you are prompted to save the files, either save them to a location of your choice, or click Cancel to skip saving.
  22. Switch to Visual Studio 2005.
  23. On the Tools menu, click Attach to Process.
  24. Select VB6.EXE (Figure 5).

    Click here for larger image

    Figure 5. Attaching the Visual Studio 2005 debugger to the Visual Basic 6.0 IDE (Click on the image for a larger picture)

  25. Click Attach.
  26. Switch to Form1 (the running Visual Basic 6.0 application)
  27. Click the button on the form. You will stop at the Visual Basic 6.0 breakpoint.
  28. Press F8 two times.
  29. Switch to Visual Studio 2005.

    You can see that you are now debugging the Visual Basic .NET class that's been called from Visual Basic 6.0. In this way, you can debug hybrid applications that call into .NET from COM environments.

  30. Press F5 to continue execution. The Hello World message box will appear (Figure 6).

    Aa730840.dbugvb06(en-US,VS.80).gif

    Figure 6. Visual Basic 6.0 application that calls into .NET

Conclusion

In this article, you have seen how you can build and debug hybrid applications. You can build Visual Basic .NET applications that call into COM components, and you can debug both the Visual Basic .NET application and the COM object that it calls into. You can also enhance your existing Visual Basic 6.0 application by calling into Visual Basic .NET classes. Again, both the application and the .NET class can be debugged.

About the author

Scott Swigart spends his time consulting, authoring, and speaking about converging and emerging technologies. With development experience going back over 15 years, and by staying in constant contact with future software development technologies, Scott is able to help organizations get the most out of today's technology while preparing to leverage the technology of tomorrow. Scott is also the author of several .NET books, a certified Microsoft trainer (MCT) and developer (MCSD), and a Microsoft MVP. Feel free to contact the Scott at scott@swigartconsulting.com, or check out his latest musings at blog.swigartconsulting.com.

Continue to the next article.

Show: