Migrating Visual Basic 6 Windows Applications

Because migrating a really trivial application would be misleading and a really long application would easily consume dozens, perhaps hundreds, of pages, I picked a sample application to migrate. The application consists of a separate client and server and is available with MSDN. (You will need to have MSDN for Visual Studio 6 installed, or you can acquire a copy of the source code from Microsoft to repeat the steps described here.)

Table A.1. VB6 Features Unsupported in VB .NET



OLE Container Control

Leave it behind.

Dynamic Data Exchange (DDE)

Consider using the SendMessage API method.


Consider upgrading to ADO in VB6 or ADO.NET in VB .NET. (You can use regular ADO in .NET through COM Interop.)


Upgrade VB5 projects to VB6 and then migrate to .NET.

ActiveX DHTML page applications

These should not be upgraded, but they do interoperate with VB .NET since you can navigate between ActiveX DHTML pages and ASP.NET Web pages.

ActiveX documents

Leave these as VB6 code. You can navigate between ActiveX documents and ASP.NET Web pages.

Property pages

The Properties window is more powerful and extensible in VB .NET, alleviating the need for property pages. For an example of this extensibility, refer to the Using the UITypeEditor Class section in Chapter 9.

User controls

VB6 user controls can be used in VB .NET, but VB6 user control projects cannot be migrated .

Web classes

Web classes can interoperate but are not automatically migrated.

Visual Basic Add-Ins

Add-Ins need to be rewritten in VB .NET.


You may need to rewrite graphics code in VB .NET with GDI+.


Graphics methods like Line and Circle are not automatically upgraded; you must reimplement them in VB .NET with GDI+ classes.

Drag-and-drop functionality

Drag-and-drop functionality must be reimplemented in VB .NET.


Variant data types are converted to the new Object class; there may be some subtle differences between how a variant is evaluated and how an object is evaluated. Pay close attention to code that relies on variants.

Windows API

Many API calls will need to be revised; the .NET framework contains objects and methods that provide a lot of functionality that VB6 could access only through Windows API calls (for example, accessing the registry).

The two programs that comprise the application I elected to migrate were in my Visual Studio files in C:\Program Files\Microsoft Visual Studio\MSDN98\98VSa\1033\SAMPLES\VB98\misc\booksale\Client\BOOK_CLI.VBP and C:\Program Files\Microsoft Visual Studio\MSDN98\98VSa\1033\SAMPLES\VB98\misc\booksale\Server\BOOK _SVR.VBP . (These paths might vary depending on the exact version of MSDN you have installed. Search on the project names to find the files if necessary.) This is a modestly complex client and server application. The application contains many of the most common elements, including forms, classes, and modules. I will provide an overview of the process and some of the problems that have to be reconciled, but I will not provide the listing since it would require too many pages.


The booksale.vbg group contains both the client and server application, but the migration wizard does not load automatically when you try to open a VB6 .vbg group (in fact, the .NET Open dialog won't allow you to open a .vbg file either). You must open and migrate each project individually.

Migrating the Sample Client Application

If you select FileOpenProject and select a VB6 project (a .vbp ) file, the migration wizard will start automatically. I used all the default options in each of the five steps, and this part of the process is trivial. Figures A.1 through A.5 show you when you are in the right place and help you retrace my steps if you are so inclined.

Figure A.1. This is the first step of the migration wizard, which you should see if you attempt to open a VB6 project.


Figure A.5. The migration may take a few minutes, as suggested by the progress bar.


Figure A.2. This is the first step of the migration wizard, which you should see if you attempt to open a VB6 project.


Figure A.3. Navigate to the .vbp project you want to migrate.


Figure A.4. The wizard begins the migration after you click Next on this screen.



Someone e-mailed and asked about a way to automate the migration process since he had many applications to migrate. I imagine that you could tap into the EnvDTE and write a macro to migrate several projects at once. I intentionally did not explore that option here. I think each application is different and should be evaluated independently. Even if the migration process were automated, the bulk of the work is in reconciling the code that cannot be migrated.

When you are finished migrating, you should see an entire VB .NET solution ( .sln file) with all the migrated files, references to COM Interop assemblies, and a file named _UpgradeReport.htm . The upgrade report is a good place to start. The _UpgradeReport.htm file (Figure A.6) provides you with a list of migrated files. Expand the list (Figure A.7) to see specific problems with a hyperlink description. Click on the linked description to navigate to a specific help topic that will explain what the problem is and recommend a resolution.

Figure A.6. The _UpgradeReport.htm file is the place to start after finishing the migration wizard.


Figure A.7. Expand migrated files in the _UpgradeReport.htm file to navigate to the help link that documents the migration error and resolution.



You can select ViewShow TasksAll in Visual Studio .NET to see all the items added to the task list during migration.

A reasonable approach is to resolve all the items in the task list and get the code to compile. Then you will need to retest the application for logic errors. For example, calls to IsObject with a variant in VB6 are migrated to IsObject with an Object type in VB .NET, and the results are evaluated differently. Carefully retest your entire application.

Resolving Migration Errors

When I ran the migration wizard I had only four bonafide compile errors. Everything else was a warning. After fixing the errors and compiling I addressed the warnings to determine whether they were really errors waiting to happen and to figure out what could be done to resolve them. Professionally, I have a rule: I don't ship code with errors, warnings, or hints. Everything gets resolved 100 percent, one way or another.

To view compiler errors and resolve those first, select ViewShow TasksBuild Errors. This filters the task list to show you everything that is preventing your application from compiling.

The first error I tackled was " frmCogs is a type and cannot be used as an expression." By examining frmCogs we can determine that frmCogs has been migrated to a WinForms object; hence we must know how to rewrite this code in VB .NET. Listing A.1 contains the method in question, and Listing A.2 contains the revised code, which resolves the error.

Listing A.1 An Improper Way to Load a Windows Form
 Private Sub cmdCogs_Click(ByVal eventSender As System.Object, _   ByVal eventArgs As System.EventArgs) Handles cmdCogs.Click   goStatusPanel.Text = "Determining Cost of Goods."   'UPGRADE_ISSUE: Load statement is not supported. Click for more:   'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1039"'   Load(frmCogs)   goStatusPanel.Text = "" End Sub 

Clicking on the hyperlink in the _UpgradeReport.htm file takes you to the VS .NET help topic ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbup1039.htm . This help topic shows you VB6 code, what the VB .NET migrated code should look like, and a remedy. The problem I encountered , in short, is that the Load method isn't used to load forms in VB .NET. Forms are classes, and we use the new operator and invoke methods on forms just like any other class. Listing A.2 shows a revision that is correct and resolves the error.


You can click the hyperlink embedded in the UPGRADE_ISSUE remark to navigate to help documentation that will aid you in resolving the problem, too.

Listing A.2 Fixing the Load Error
 Private Sub cmdCogs_Click(ByVal eventSender As System.Object, _   ByVal eventArgs As System.EventArgs) Handles cmdCogs.Click   goStatusPanel.Text = "Determining Cost of Goods."   'UPGRADE_ISSUE: Load statement is not supported. Click for more:   'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1039"'   Dim form As frmCogs = New frmCogs()   goStatusPanel.Text = "" End Sub 

Typically in .NET we create a form with the New operator and then call Form.Show or Form.ShowDialog . The migrated code actually contains the call to Show in the Load method, so we didn't need to do it here.

There was one more error like this plus two errors related to a mouse click event. In all, migrating BOOK_CLI.VBP took about five minutes, and resolving the basic errors took another five minutes. Thus after ten minutes the client application compiled and ran. In addition, because in VB .NET we can talk to VB6 servers, I was able to run both the client and the server by converting only the client. Figures A.8 and A.9 show the client and server running, respectively.

Figure A.8. The migrated client application running as a VB .NET assembly.


Figure A.9. The server application running as a VB6 executable, interacting with the .NET migrated client.


At this juncture we still clearly need to evaluate all the UPGRADE_ISSUE task items and resolve those. However, we have a choice, relative to the server. If the server is running, we can evaluate it as a separate application and decide whether or not to migrate it. It all depends on your needs, budget, time constraints, and priorities.

The number and variety of potential errors are too great to provide a comprehensive list of problems and resolutions . The best approach is to really understand the .NET Framework and in the meantime rely on the _UpgradeReport.htm file and the help documentation. Microsoft claims that the migration wizard will migrate about 95 percent of VB6 code, which should put you in pretty good shape.

Visual Basic. NET Power Coding
Visual Basic(R) .NET Power Coding
ISBN: 0672324075
EAN: 2147483647
Year: 2005
Pages: 215
Authors: Paul Kimmel

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