Upgrading a Visual Basic 6 ClientServer Application

Upgrading a Visual Basic 6 Client/Server Application

Suppose you have a Visual Basic 6 ActiveX DLL that acts as a language translator, translating a phrase in one language to another language. Your component is currently limited to rudimentary Spanish. The companion CD includes two Visual Basic 6 sample applications: TranslatorServer and TranslatorClient. In the following section we will demonstrate how to call the TranslatorServer component from a Visual Basic .NET client application. First, however, you need to build the Visual Basic 6 server component by following these steps:

  1. Run Visual Basic 6.

  2. Open TranslatorServer.Vbp provided on the companion CD.

  3. Open TranslatorServer.Cls, and you will find that it contains the following code:

    Public Function Translate(ByVal SentenceFrom As String, _ ByVal LanguageFrom As String, _ ByVal LanguageTo As String) As String    ' Note to self: Find someone who speaks Spanish and VB who     ' is willing to expand this component to translate any  ' common English phrase    If LCase(LanguageFrom) = "english" And _       LCase(LanguageTo) = "spanish" Then       Select Case LCase(SentenceFrom)          Case "hello world"             Translate = "hola mundo"       End Select    End If End Function

  4. From the File menu, choose Make TranslatorServer.dll and make the .DLL file into a directory on your hard drive.

To test your application in Visual Basic 6, run Visual Basic 6 and open the Visual Basic 6 TranslatorClient application provided on the companion CD. Open Module1.Bas and you ll find the following code to call the TranslatorServer component.

Sub Main()    Dim Translation As String      Dim Ts As New TranslatorServer.Translator    Translation = Ts.Translate("hello world", "English", "Spanish")    MsgBox Translation End Sub

Now select References from the Project menu, click the Browse button, and locate the TranslateServer.Dll that you built by following the previous steps. Press F5 to run the project. You should see "hola mundo" displayed, as shown in Figure 9-3.

  1. Figure 9-3

    Hola mundo translation successfully received from Visual Basic 6 server.

You may be wondering what the point of all this is. So far, all we ve done is call a Visual Basic 6 component from Visual Basic 6. Now, however, we ll upgrade each component separately to see how to call a Visual Basic 6 component from Visual Basic .NET and vice versa. Finally, we ll see how to tie the upgraded client and server components together to form a complete Visual Basic .NET solution.

Creating a .NET Client That Talks to a COM Server

Let s create a .NET client that talks to a COM server. Since the whole point of Visual Studio .NET is to help you create applications quickly for the Web, let s create a Web client that calls our COM server. You might want to do this, for example, if you have business logic stored internally say, a Visual Basic 6 ActiveX DLL function that returns a list of your company s products that you want to make available for viewing by external partners or customers.

  1. Run Microsoft Visual Studio .NET.

  2. Choose New Project from the File menu.

  3. Select Visual Basic ASP.NET Web Application, name the application MyAmazingTranslator, and click OK.

  4. Right-click References on the Solution Explorer tab, and choose Add Reference.

  5. Select the COM tab.

  6. Select TranslatorServer from the list, and click OK.

  7. Open WebForm1.aspx.

  8. Drag and drop the following controls from the Toolbox to WebForm1: A Label, a TextBox, and a Button.

  9. For the text of the label, enter My amazing English-to-Spanish translator.

  10. For the text of Button1, enter Translate.

  11. Double-click the Translate button and insert the following code in the Button1_Click event handler:

    Dim ts As New TranslatorServer.Translator() TextBox1.Text = ts.Translate(TextBox1.Text, "English", _    "Spanish")

  12. Choose Start from the Debug menu.

  13. Type hello world into the text box, and click the button. See Figure 9-4 for an example of the output.

You have now taken a simple desktop application and made it available to the world. Hola mundo indeed.

Figure 9-4

My amazing English-to-Spanish translator at work on the Web.

Debugging Between the Visual Basic .NET Client and Visual Basic 6 Server

When upgrading Visual Basic 6 code to Visual Basic .NET, it is critical that you be able to debug the changes that you made yourself or that were made by the Upgrade Wizard. The Microsoft Visual Studio .NET development environment makes it possible to debug between Visual Basic 6 applications and Visual Basic .NET. However, you need to make a few changes to your Visual Basic 6 code in order to debug it using the Visual Studio .NET debugger.

Preparing Your Visual Basic 6 Project to Debug Using Visual Studio .NET

To be able to set breakpoints in your Visual Basic 6 source code, you need to build your Visual Basic 6 project with debugging symbols turned on. To do so, perform the following steps:

  1. Run Visual Basic 6 and load the application you want to debug. In this case, load TranslatorServer.vbp.

  2. From the Project menu, choose TranslatorServer Properties.

  3. Click the Compile tab and select Create Symbolic Debug Info.

  4. For best results, select No Optimization. See Figure 9-5.

    Figure 9-5

    Recommended Visual Basic 6 options for debugging.

    At this point it s a good idea to turn on binary compatibility so that you do not need to update your .NET client application.

  5. Click the Component tab.

  6. Change the Version Compatibility setting from Project Compatibility to Binary Compatibility, as shown in Figure 9-6.

  7. Click OK to close the Project Properties dialog box.

Now rebuild your application. Choose Make TranslatorServer.dll from the File menu, click OK, and click Yes to replace the existing file, if prompted to do so.

Figure 9-6

Setting binary compatibility in Visual Basic 6.

Now you re ready to debug.

  1. Run Visual Studio .NET and open the MyAmazingTranslator Web project you created earlier.

  2. Select the MyAmazingTranslator project in Solution Explorer.

  3. From the Project menu, choose Properties.

  4. Expand the Configuration Properties folder and select Debugging.

  5. Under Debuggers, click Unmanaged Code Debugging.

  6. Click OK to close the dialog box.

  7. Open WebForm1.aspx, and double-click the Translate button.

  8. Insert a breakpoint on the following line:

    TextBox1.Text = ts.Translate(TextBox1.Text, "English", _    "Spanish")

  9. From the File menu, choose Open File, and open TranslatorServer.cls, saved as part of the Visual Basic 6 TranslatorServer project.

  10. Right-click the first line of code in the Translate function, and select Insert Breakpoint.

  11. Choose Start from the Debug menu.

  12. Click the Translate button. Execution should break on the following line:

    TextBox1.Text = ts.Translate(TextBox1.Text, "English", _    "Spanish")

  13. Step over the line, and execution should break in your Visual Basic 6 TranslatorServer.cls code file. You can now step through your Visual Basic 6 code to ensure that everything is working properly.

    note

    In order to debug an ASP.NET application on your local machine, you need to be added as a member of the Debugger Users group. If you do not have administrative privileges on the machine, you also need to change the Machine.config file for Aspnet _wp.exe to run Aspnet_wp.exe with User privileges rather than System account privileges. See Visual Studio .NET s Help system for more details.

Exposing a Visual Basic .NET Component to Be Called by a Visual Basic 6 Client

In some cases, you will want to upgrade your Visual Basic 6 server and make it available to your Visual Basic 6 or other COM client applications. For example, since Visual Basic .NET allows you to create multithreaded components, you may want to upgrade your Visual Basic 6 component to a .NET component. You can then register the component to take advantage of a multithreaded environment such as Microsoft Transaction Server (MTS) in order to use object pooling, for example.

Let s take the Visual Basic 6 TranslatorServer component located on the companion CD and upgrade it to Visual Basic .NET by performing the following steps:

  1. Run Visual Studio .NET.

  2. From the File menu, choose Open Project and open TranslatorServer.vbp.

  3. Step through the Upgrade Wizard by clicking Next and selecting the default options as you go.

    Let s change the name of the .NET server so that it doesn t conflict with the server name of its COM predecessor.

  4. Select the TranslatorServer project in the Solution Explorer.

  5. From the Project menu, choose Properties.

  6. Change the Assembly Name from TranslatorServer to TranslatorServer.net.

  7. Click OK to close the Project Properties dialog box.

  8. View the code for TranslatorServer.vb:

    Option Strict Off Option Explicit On Public Class Translator     Public Function Translate(ByVal SentenceFrom As String, _ ByVal LanguageFrom As String, ByVal LanguageTo As String) _ As String              ' Note to self: Find someone who speaks Spanish and VB       ' who is willing to expand this component to translate       ' any common English phrase       If LCase(LanguageFrom) = "english" And _          LCase(LanguageTo) = "spanish" Then          Select Case LCase(SentenceFrom)             Case "hello world"                Translate = "hola mundo"          End Select       End If           End Function End Class

Note that the upgraded code, specifically the contents of the Translate function, is exactly the same as the Visual Basic 6 code. The point is that you can create a Visual Basic .NET server in exactly the same way that you create a Visual Basic 6 ActiveX DLL server. Now for the gotcha. Although you can build the upgraded Visual Basic .NET server, you cannot call it from Visual Basic 6. Why not? Because by default a Visual Basic .NET server is meant to be called by other .NET components, not by a COM component. To call a Visual Basic .NET server from a COM application such as Visual Basic 6, you need to register the server for COM. The simple way to expose the component to COM is to turn on the Register For COM Interop attribute by doing the following:

  1. Select the TranslatorServer project in the Solution Explorer.

  2. From the Project menu, choose Properties.

  3. Under the Configuration Properties folder, select Build.

  4. Select the Register For COM Interop check box, as shown in Figure 9-7, and click OK to close the dialog box.

  5. From the Build menu, choose Build Solution, and click the Save button to save the Solution file. This step creates the Visual Basic .NET DLL and also registers it for COM.

    Figure 9-7

    Registering the component for COM interop.

note

Registering a .NET component for COM interop does not necessarily mean that only COM clients can use the component. Instead, it opens that component up to the world of both COM and .NET. Exposing your .NET server to COM allows you to upgrade old clients or to create new clients in .NET that use the COM server. At the same time you can make a quick modification to other COM clients to allow them to work with your .NET server.

Now you are ready to call the Visual Basic .NET TranslatorServer component from Visual Basic 6. This process involves the same steps used to create the Visual Basic 6 TranslatorClient application located on the companion CD. To call the Visual Basic .NET TranslatorServer component from Visual Basic .NET, follow these steps:

  1. Run Visual Basic 6 and open the TranslatorClient application located on the companion CD.

  2. Choose References from the Project menu.

  3. Deselect TranslatorServer, the Visual Basic 6 server component.

  4. Select TranslatorServer_net, the Visual Basic .NET server component.

  5. Open TranslatorClient.bas.

  6. Change the following declaration from

    Dim Ts As New TranslatorServer.Translator

    to

    Dim Ts As New TranslatorServer_net.Translator

  7. Run the application.

You should see the results shown in Figure 9-3.

note

If you want to add features to your upgraded Visual Basic .NET component, you should add COM attributes to ensure binary compatibility. Binary compatibility causes your Visual Basic .NET component to look, act, and smell like existing COM clients no matter what new features you add to the component. If you break compatibility, your COM clients will not be able to find the .NET component. You will be forced to recompile your COM client against the updated .NET server and redistribute it. For an example of how to add COM attributes to ensure binary compatibility, see Replacing COM with .NET: Binary Compatibility later in this chapter.

Debugging Between the Visual Basic 6 Client and .NET Server

Earlier we showed you how to debug both Visual Basic 6 and Visual Basic .NET code using the Microsoft Visual Studio .NET debugger. Now we re going to debug our Visual Basic 6 client application and .NET server, using both the Visual Basic 6 debugger and the Visual Studio .NET debugger to step across the call from Visual Basic 6 code to Visual Basic .NET code. Let s start in the Visual Studio .NET development environment.

  1. Run Visual Studio .NET and open the TranslatorServer application you upgraded to Visual Basic .NET earlier.

  2. Open TranslatorServer.vb and place a breakpoint within the Translate function on the following line:

    If LCase(LanguageFrom) = "english" And _    LCase(LanguageTo) = "spanish" Then

  3. Select the TranslatorServer project in Solution Explorer.

  4. From the Project menu, choose Properties.

  5. Under the Configuration Properties folder, select Debugging.

  6. Select Start External Program, click the Browse ( ) button, and search for VB6.exe.

  7. From the Debug menu, choose Start. Visual Basic 6 will launch.

  8. From Visual Basic 6, open TranslatorClient.vbp.

  9. Place a breakpoint on the following line:

    Translation = Ts.Translate("hello world", "English", _    "Spanish")

  10. Run the Visual Basic 6 client application.

Execution will break in the Visual Basic 6 application. Step over the line of code.

The Visual Studio .NET debugger will appear, and execution will break in the Visual Basic .NET server application. You can step through and debug your Visual Basic .NET server application code. When execution returns from the Visual Basic .NET server, function execution resumes in the Visual Basic 6 debugger. Pretty cool, eh?

Tying It All Together

As you have seen, COM interop enables you to upgrade your application one piece at a time. You can choose to upgrade one part to .NET while keeping other parts based on COM. If your goal is to move your application to the Web or an intranet environment, plenty of options are available to you. For example, you can create a Web front end that uses the same back-end logic currently in use by your traditional Windows client applications. You can also make your back-end functions your business logic available to remote clients on the Internet. To do so, you can expose your functions as Web services. In the case of the Translator class, you can expose the Translate function over the Web by adding a Web service class to the project, copying the contents of the Translator class to the Web service class, and marking the Translate function as a WebMethod. We leave this task as an exercise for you to complete.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net