Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Professional Visual Studio 2005 (2006) [eng]

.pdf
Скачиваний:
132
Добавлен:
16.08.2013
Размер:
21.9 Mб
Скачать

Chapter 45

Figure 45-9

Unflagged Issues

Some issues can crop up in your conversion that are not flagged at all. These are usually related to situations that the Upgrade Wizard cannot determine, such as differences in literal value meanings.

A great example of this is the kinds of errors that can occur at runtime. When trapping errors using the On Error statement, often code is hard-coded with specific error numbers. Because the behavior may be different in Visual Basic 2005, different errors may be produced, which can cause unexpected results. The MSDN documentation has the following sample listing that illustrates this issue:

On Local Error GoTo Result

Dim x() As Boolean

Dim y As Variant

y = x(10)

Result:

If Err.Number = 9 Then

Do something.

Else

Do something else. End If

646

Upgrading to Visual Studio 2005

When this code is run in Visual Basic 6 it will generate an Err object with a value of 9, which means “Subscript out of range.” However, because Visual Basic 2005 requires you to initialize any object (which includes arrays) before using it, you will get a different error number.

Because this kind of unseen issue can occur, it is always highly recommended to do a complete code review of upgraded projects.

The Upgrade Visual Basic 6 Tool

If you need only to convert a block of code instead of an entire project, whether it’s a single code statement or a full module, you will find the Upgrade Visual Basic 6 Code tool extremely useful. New to Visual Studio 2005, this utility is available from the Tool menu when you are editing a class or module in Code view.

When you start the utility, it displays a small dialog window containing a blank text area in which you can enter the Visual Basic 6 code to be converted (as shown in Figure 45-10). If the code uses COM objects, you can add the references to each COM object you require in the References tab.

Figure 45-10

When the code and references are ready for conversion, click the Upgrade button to start the conversion process. This uses the same internal engine as the Upgrade Wizard, and automatically inserts the newly converted Visual Basic 2005 code into the module at the current insertion point. Any necessary COM references are automatically added to the project’s reference list.

Some additional errors may occur when converting a small block of Visual Basic code, such as converting a statement that includes an undefined variable. To avoid these, try to include everything that the particular code will need prior to clicking the Upgrade button.

647

Chapter 45

Summar y

Visual Studio 2005 comes with two utilities to migrate your Visual Basic 6 code over to the new language constructs — an Upgrade Wizard to convert complete projects to Visual Basic 2005, and a code snippet converter utility for converting individual functions or single modules of code. The two tools in conjunction enable you to upgrade Visual Basic 6 code more easily than ever, and along with the new features of Visual Studio 2005 (such as edit-and-continue), pave the way for Visual Basic 6 programmers to transition to .NET.

648

Build Customization

While you can build most of your projects using the default compilation options set up by Visual Studio 2005, occasionally you’ll need to modify some aspect of the build process to achieve what you want. This chapter looks at the various build options available to you in both Visual Basic and C#, outlining what the different settings do so you can customize them to suit your own requirements.

In addition, you’ll learn how Visual Studio 2005 uses the new MSBuild engine to perform its compilations and how you can get under the hood of the configuration files that control the compilation of your projects.

General Build Options

Before you even get started on a project, you can modify some settings in the Options pages for Visual Studio 2005. These options apply to every project and solution that you open in the IDE, and as such can be used to customize your general experience when it comes to compiling your projects.

The first port of call for professional Visual Basic developers should be the General page of the Projects and Solutions group. By default, the Visual Basic setup of the IDE hides some of the build options from view, so the only way to show them is to activate the Show Advanced Build Configurations option.

When this is active, the IDE displays the Build Configuration options in the My Project pages, and the Build Configuration Manager menu command is also accessible. Other language environments don’t need to do this as these options are activated on startup (although you could certainly turn them off if you didn’t want them cluttering your menus and pages).

Chapter 46

The other option on this page relating to building your projects is whether or not to automatically show the Error List if compilation errors are encountered during the build process. In this case, all language configurations have the option turned on.

The Build and Run options page (shown in Figure 46-1) in the Projects and Solutions group has many more options available to you to customize the way your builds take place.

Figure 46-1

Under some environment setups, the Show All Settings option will not be visible. It’s only there to simplify Options access for Visual Basic developers.

It’s unclear from this page, but some of these options affect only C++ projects, so it’s worth running through each option, what it does, and what languages it affects:

Before building: This tells Visual Studio how to handle changes that have been made to any part of your project prior to the build process. You have three options:

Save all changes automatically saves any changes without prompting you. This is perhaps the best option, as you don’t have to remember to save your work. This is the default setting.

Save changes to open documents only also automatically saves changes, but only to open documents. Previous versions of Visual Studio would not allow you to close an edited file without either saving it or canceling the changes. However, Visual Studio 2005 allows you to do so.

Prompt to save changes to open documents gives you the chance to save any changes before the build commences. When the build process is started it displays a dialog prompting you to save the changes or not. If you decline to save the changes, the build

650

Build Customization

still continues but uses the last saved version of the file. This option can be good to use when you want to know when you’ve made changes (perhaps inadvertently) to the source code.

Maximum number of parallel project builds: This controls how many simultaneous build processes can be active at any one time (assuming the solution being compiled has multiple projects). This option only affects how C++ solutions are built and has no effect on Visual Basic or C# projects.

Only build startup projects and dependencies on Run: This option only builds the part of the solution directly connected to the startup projects. This means that any projects that are not dependencies for the startup projects are excluded from the default build process. This option is active by default, so if you’ve got a solution that has multiple projects called by the startup projects through late-bound calls or other similar means, they will not be built automatically. You can either deactivate this option or manually build those projects separately.

On Run, when projects are out of date: This option is used for C++ projects only and gives you three options for out-of-date projects (projects that have changed since the last build). The default is Prompt to Build, which gives you an option to build for each out-of-date project. The Always Build option forces the build process to occur whenever you run the application, while the Never Build option always uses the previous build of out-of-date projects. Note that

this only applies to the Run command, and if you force a build through the Build menu, projects are rebuilt according to the other settings in the build configuration and on this Options page.

On Run, when build or deployment errors occur: This controls the action to take when errors occur during the build process. Despite official documentation to the contrary, this option does indeed affect the behavior of builds in Visual Basic and C#. Your options here are the default Prompt to Launch, which displays a dialog prompting you for which action to take; Do Not Launch, which does not start the solution and returns to design time; and Launch Old Version, which ignores compilation errors and runs the last successful build of the project.

The option to launch an old version enables you to ignore errors in subordinate projects and still run your application; but because it doesn’t warn you that errors occurred, you run the risk of getting confused about what version of the project is active.

Note that when you use the Prompt to Launch option, if you subsequently check the “Do not show this dialog again” option in the prompt dialog, this setting will be updated to either Do Not Launch or Launch Old Version depending on whether you to choose to continue or not.

For new solutions use the currently selected project as the startup project: This option is useful when you’re building a solution with multiple projects. When the solution is being built, the Visual Studio build process assumes that the currently selected project is the startup project and determines all dependencies and the starting point for execution from there.

MSBuild project build output verbosity: Visual Studio 2005 uses the new MSBuild engine for its compilation. MSBuild produces its own set of compilation output, reporting on the state of each project as it’s built. You have the option to control how much of this output is reported to you:

By default, the MSBuild verbosity is set to Minimal, which produces only a very small amount of information about each project, but you can turn it off completely by setting this option to Quiet, or expand on the information you get by choosing one of the more detailed verbosity settings.

651

Chapter 46

MSBuild output is sent to the Output window, which is accessible via View Other Windows Output (under some environmental setups this will be View Output). If you can’t see your build output, make sure you have set the Show Output From option to Build (see Figure 46-2).

Figure 46-2

It’s also worth taking a look at the other Options pages in the Projects and Solutions category, as they control the way Visual Basic deals with its compilation options by default (Option Explicit, Option Strict, and Option Compare), and other C++-specific options relating to build.

Of note for C++ developers is the capability to specify PATH variables for the different component types of their projects, such as executables and include files, for different platform builds; and whether or not to log the build output.

Batch Building

You can perform batch builds of multiple project configurations. If your project has multiple configurations defined, an additional command is displayed on the Build menu to perform a Batch Build. The Batch Build dialog enables you to select which configurations should be built from those defined in the solution (see Figure 46-3). For instance, you can build a debug version of the application, complete with debugging symbols, at the same time as a full release version of the solution, without any debugging and trace information.

Manual Dependencies

You can also set manual project dependencies to indicate how projects are related to each other. This is useful if you’re using the “Only build startup projects and dependencies on Run” setting to enforce the compilation of your late-bound projects. You can access the dialog shown in Figure 46-4 by selecting either the Project Project Dependencies or Project Build Order menu commands.

Note that these menu commands are only available when you have a solution with multiple projects in the IDE.

652

Build Customization

Figure 46-3

Figure 46-4

You first select the project that is dependent on others from the drop-down, and then check the projects it depends on in the bottom list. Any dependencies that are automatically detected by Visual Studio 2005 will already be marked in this list. The Build Order tab can be used to confirm the order in which the projects will be built.

653

Chapter 46

Visual Basic Compile Page

Visual Basic projects have an additional set of options that control how the build process will occur. To access the compile options for a specific project, open My Project by double-clicking its entry in the Solution Explorer. When the project Options page is shown, navigate to the Compile page from the list on the left side (see Figure 46-5).

Figure 46-5

The Build Output Path option controls where the executable version (application or DLL) of your project is stored. For Visual Basic, the default setting is the bin\Release\ directory, but you can change this by browsing to the desired location. The majority of the other settings on this page are dealt with in other chapters. Particularly, the conditions and warning-related options are discussed in Chapter 29.

You should be aware of two additional sets of hidden options. The Build Events button in the lowerright corner is available to Visual Basic developers who want to run actions or scripts before or after the build has been performed. They are discussed in a moment.

Advanced Compiler Settings

Clicking the Advanced Compile Options button displays the Advanced Compiler Settings dialog (see Figure 46-6) in which you can fine-tune the build process for the selected project, with settings divided into two broad groups: Optimizations and Compilation Constants.

654

Build Customization

Figure 46-6

Optimizations

The settings in the Optimizations group control how the compilation is performed to make the built output or the build process itself faster or to minimize the output size. Normally, you can leave these options alone, but if you do require tweaks to your compilation, here’s a summary of what each option does:

Remove integer overflow checks: By default, your code is checked for any instance of a possible integer overflow, which can be a potential cause for memory leaks. Deactivating this option removes those checks, resulting in a faster-running executable at the expense of safety.

Enable optimizations: The default behavior for Visual Basic projects is to disable all compiler optimizations to ensure an accurate execution, but you can enable the optimizations, which results in a faster-running executable that may not have an exact behavior due to the differences in the optimization code.

DLL base address: Used for, you guessed it, DLLs, this option enables you to specify the base address of the DLL in hexadecimal format. This option is disabled when the project type will not produce a DLL.

Generate debug info: This controls when debug information will be generated into your application output. By default, the option is set to pdb-only, which will only produce PDB debugging information, but you can either turn debugging information off completely or set the option to Full to include additional debugging sources.

655