نکته ای درمورد. Debugging Hybrid Visual Basic 6.0/Visual Basic .NET Applications
نقل قول:
Debugging Hybrid Visual Basic 6.0/Visual Basic .NET Applications
Visual Studio 2005
Scott Swigart
Swigart Consulting LLCJuly 2006Applies to:
Microsoft Visual Basic 6.0
Microsoft Visual Basic 2005
Microsoft Visual Studio 2005Summary: This articlesshows 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.
- Open Visual Studio 2005.
- On the File menu, click New, and then click Project.
- Create a new Windows Application project named VBNET App.
- 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).
https://i-msdn.sec.s-msft.com/dynimg/IC38310.gif
Figure 1. Simple Visual Basic .NET application calling into a Visual Basic 6 COM object
- Start Visual Basic 6.0.
- Create a new ActiveX DLL project.
- Add the following code to Class1.Public Function HelloWorld() As String
HelloWorld = "Hello World"
End Function
- On the File menu, click Make Project1.dll.
- 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.
- Switch to Visual Studio 2005.
- On the Project menu, click Add Reference.
- Click the COM tab.
- Select the Project1 component (Figure 2).https://i-msdn.sec.s-msft.com/dynimg/IC70004.gif
Figure 2. Adding a reference to the Visual Basic 6.0 component
- 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.
- On the Form1 designer, double-click the button to generate the code for the click event handler.
- 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
- Press F5 to run the application. When you click the button, you should see the Hello World message box (Figure 3).https://i-msdn.sec.s-msft.com/dynimg/IC98943.gif
Figure 3. Visual Basic .NET application calling a Visual Basic 6.0 COM object
- Close the application.Now it's time to debug the Visual Basic .NET application and the Visual Basic 6.0 component that it calls.
- Switch to Visual Basic 6.0.
- Set a breakpoint on the following line of code.HelloWorld = "Hello World"
- Press F5.
- In the Project1 - Project Properties dialog box, click OK.
- 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.
- Switch to Visual Studio 2005.
- Set a breakpoint on the following line of code.Dim c As New Project1.Class1
- Press F5 to run the application.
- Click the button. Execution should stop at the break point in your Visual Basic .NET application.
- On the Debug menu, click Step Into.
- On the Debug menu, click Step Into again.
- 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.
https://i-msdn.sec.s-msft.com/dynimg/IC57847.gif
Figure 4. Debugging a Visual Basic 6.0 COM component called from Visual Basic .NET
- Stop the Visual Basic 6.0 and Visual Basic .NET debuggers.
- 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.
- Start Visual Studio 2005.
- On the File menu, click New, and then click Project.
- For the project template, select Class Library.
- Name the project VBNET Component, and then click OK.
- On the Project menu, click Add New Item.
- 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.
- Add the following code to ComClass1. Public Function HelloWorld() As String
Return "Hello World"
End Function
- On the Build menu, click Build Solution.This compiles the Visual Basic .NET project and registers ComClass1 as a COM object.
- Start Visual Basic 6.0.
- In the New Project dialog box, select Standard EXE and click Open.
- Add a Command button control to the form.
- On the Project menu, click References.
- Select VBNET_Component and click OK.
- On the Form1 designer, double-click the button to generate the event handler.
- 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
- Set a breakpoint on the following line of code. Set c = New VBNET_Component.ComClass1
- Switch to Visual Studio 2005.
- 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.
- Switch to Visual Basic 6.0.
- Press F5 to run the application.
- If you are prompted to save the files, either save them to a location of your choice, or click Cancel to skip saving.
- Switch to Visual Studio 2005.
- On the Tools menu, click Attach to Process.
- Select VB6.EXE (Figure 5).https://i-msdn.sec.s-msft.com/dynimg/IC77864.gif
Figure 5. Attaching the Visual Studio 2005 debugger to the Visual Basic 6.0 IDE (Click on the image for a larger picture)
- Click Attach.
- Switch to Form1 (the running Visual Basic 6.0 application)
- Click the button on the form. You will stop at the Visual Basic 6.0 breakpoint.
- Press F8 two times.
- 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.
- Press F5 to continue execution. The Hello World message box will appear (Figure 6).https://i-msdn.sec.s-msft.com/dynimg/IC43268.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