Optimizing and Deploying an Application
*Certification Objectives
*Optimizing an Application
*Tips and Tricks of Optimization
*Selecting the Appropriate Compiler Options
*Using Conditional Compilation to Control an Application
*Monitoring the Values of Expressions and Variables Using the Debug Window
*Using the Immediate Window to Check or Change Values
*Setting Watch Expressions During Program Execution
*Defining the Scope of a Watch Variable
*Using the Locals Window to Check or Change Values
*Implementing Project Groups to Support the Development and Debugging Process
*Debugging DLLs in Process
*Testing and Debugging a Control in Process
*Deploying an Application
*Using the Package and Deployment Wizard to Create a Setup Program to Install a Desktop Application, Register the COM Components, and Allow for Uninstall
*Creating a Setup Program for Desktop Application Installation
*Registering the COM Components in a Desktop Application
*Manipulating the Windows Registry
*Allowing for Uninstall
*Planning and Implementing Floppy DiskBased Deployment or Compact DiscBased Deployment for a Desktop Application
*Floppy DiskBased Deployment of a Desktop Application
*Compact DiscBased Deployment of a Desktop Application
*Planning and Implementing Web-Based Deployment for a Desktop Application
*Deploying an ActiveX Control
*Planning and Implementing Network-Based Deployment for a Desktop Application
*Certification Summary
*From the Classroom
*Optimization and Deployment
*Two-Minute Drill
*Self Test
*
Theres not much point to creating an application if people cant use it. Thats the entire point of optimizing, debugging, and deploying an application. It allows you to set how you want your program to run, make sure it runs properly, and get it out to the user.
Optimization allows you to get the most out of your application. You can optimize for speed or performance. While optimization is a bit of a trade-off, Visual Basic version 6 allows quick and easy options that help you to create an application that runs the way you want it to.
Debugging allows you to test for runtime and logic errors through tools that come with VB 6. This allows you to test for problems before compiling and deploying your program.
Also in this chapter, well cover how to package and deploy a program so it gets from your desktop to someplace that users can install it on theirs. This includes floppy disk, CD-ROM, local and network folders, and deployment to an Internet Web site.
There are three basic rules to optimization, and none of them are very appealing. The first rule is that no matter how many tricks and tools you use to optimize performance, it will always run poorly if the design is poor. While we covered design in Chapter 5, this aspect can never be stressed enough.
You need to put the users needs before your own. While it may look great to have a high-resolution picture thats enormous in size as the background of your forms, is it really needed? Just because you think something should be added to a program, is it the general consensus? A cornerstone of design is asking what the program is supposed to do. What does the user need to get out of the program? This information can be obtained by talking to your target users. Finding out what the users needs, and then designing the application for performance is the first step to optimization.
This leads to the second rule of optimization: It is important to determine what factors are important to the user. You cant tune an application if you dont know what the user needs. If users of an application have computers that are limited in memory, then it does little good to increase performance by loading every form into memory at once. In short, you need to understand the problems before you can optimize them.
After getting the necessary information, you should then outline your application in a flowchart program, a word processor, or on a piece of paper. Figure out what forms need to be created, what controls should appear on the form, and how different components of the program will interact. A common mistake is adding too many controls on a form. Not only does this make the form more confusing for the user, but it eats up memory and slows performance. When the form is loaded, each control has to be loaded. If the controls access data from a database, each control must access data as the user moves through records. By designing your application properly before coding takes place, you can increase its performance dramatically.
The third rule of optimization is that optimization is a trade-off. Increasing performance in one area can often increase the overhead of another resource. To use the example of forms, an application will run faster if each form is loaded into memory, but memory is sacrificed. This makes optimization a bit of a balancing act. The trick is to increase performance as make it as good as possible, but know when to quit. It is easy to keep trying to optimize an application, losing the objectivity that youve optimized it as best you can.
One of the easiest ways to increase performance is by using the correct data type. As we saw in Chapter 2, there are many different data types that you can use in your applications. However, data types take up different amounts of memory. For example, lets say you used a variable to store a persons age. While you could use either an Integer or Double to store it, an Integer uses two bytes and a Double uses eight. Unless Phyllis Diller is a user, you can safely assume youll never get a person over the age of 32,767. Using anything other than an Integer would be overkill and use up memory unnecessarily. In addition, the smaller-sized data type will increase the speed of calculations. Remember the KISS method: Keep It Simple, Silly. Always use the simplest data type.
Another tip for increasing performance is to cache properties in variables when youre using a loop. It is faster to access data stored in a variable, than data stored in a property. When using a loop, you should first set the property to a variable, then use the variablerather than the property itselfin a loop. As your loop moves through the variables, it will access the data faster.
When youre done with data, you should discard it from memory. You can significantly increase performance by reclaiming the space used by arrays and variables. For example, when data isnt needed by a dynamic array, you should use Erase or Redim Preserve to discard the data. Erase is used to eliminate an array, while Redim Preserve can make the array smaller. In addition you should remove the values of variables to reclaim space. This is seen in the following example:
strMyName="My Name is Michael Cross and this is my string"
strMyName=""
In the first line, we assign a long sentence to the String variable called strMyName. In the second line, we clear the value of strMyName and reclaim the space. In doing so, memory is freed up, and performance increases.
However, removing the values of variables to reclaim space should only be done if the variables are going to be out of memory for some time. It makes no sense, and will decrease performance, if you reclaim space only to use the variable a short time later.
Graphics can be an evil beast where optimization is concerned. They gobble up memory and network bandwidth, and can affect the overall performance of applications. There are, however, ways to tame the beast and increase performance.
When a picture is no longer needed, it is advisable to clear the object. If you have a graphic shown in a picture box, you should use the LoadPicture function to clear it. This is shown in the following code:
picPicture.Picture=LoadPicture("GRAPHIC.BMP")
picPicture.Picture=LoadPicture()
The first line of this code assigns a bitmap called GRAPHIC.BMP to the Picture property of a picture box called picPicture. In the next line of code, we clear the Picture property by using LoadPicture(). This frees up memory that was previously used to store the graphic.
Picture boxes are useful for simulating groups of controls. As was mentioned, having large groups of controls eats up memory and slows performance. By using a picture to simulate a control set, you can decrease the amount of memory used.
Another way to decrease the overhead of graphics is using an Image control. The Image control uses less memory than the PictureBox control does. It should be used instead of picture boxes in cases where youre just using a graphic to respond to a Click event.
The AutoRedraw property of a form can also be used to increase performance. AutoRedraw sets the output of a graphic to either Graphics method or Persistent Graphic. Graphics method performs runtime drawing operations and is used for animation and simulations. Persistent Graphic is a method that stores output in memory and retains the graphic during screen events. An example of a screen event would be if you switched between applications in Windows and hid and redisplayed a form. When AutoRedraw is set to False, Graphics method is used, while Persistent Graphic mode is used when AutoRedraw is set to True. If your application uses or generates complex graphics, it is better to use Graphics method (setting AutoRedraw to False), and handling the repainting of graphics yourself through code.
Up until this point, weve dealt with real ways of increasing performance. Some methods are along the lines of sleight of hand. There are a number of ways to make users think performance has increased, when in fact nothing has improved.
One way of masking the speed of an application is through the use of "splash screens". As your application first loads, a form is displayed with its MinButton, MaxButton, and ControlBox properties set to False. The contents of the form can include the applications name, your company name, and so forth. Youve seen these types of screens when you load VB. A form telling you the program name is displayed as the application loads. This not only masks the time spent for a large application to load but also lets the user know that something is happening. Without this, a user might think that nothing has happened and try to start the application again.
Another way to let users know that an application hasnt locked up on them is by using progress indicators. The professional and enterprise editions of VB include the ProgressBar control in the Microsoft Windows Common Controls. After adding this control to a form, you can then use DoEvents at points in your program to update the value of the progress bar. The point of doing this is to let the user know that the application is working. This is especially useful when doing such things as populating a large array or loading a particularly large database. If you decide not to use a progress bar, you should at least change the mouse icon to an hourglass.
Finally, while its been mentioned here about the problems of loading multiple forms into memory, you should load commonly used forms and keep them hidden. Hidden forms display quicker than forms that are loaded from disk. This doesnt mean to load every form in an applicationjust the common ones a user will use. For example, you shouldnt hide things like About this Program or forms that supply configuration options. They arent used often, and eat up unnecessary memory.
Exercise 11-1: Optimizing a Program
Unload Form1
Form2.Show
Dim counter As Integer
Dim intNum(100) As Integer
For counter = 1 To 100
intNum(counter) = counter * 5
ProgressBar1.Value = counter
Next counter
ProgressBar1.Value = ProgressBar1.Min
VB allows you to control how your project is compiled into an executable. It also allows you to tweak your application, so it is optimized in a way that best suits your needs and the needs of your user. The options you can set are found under the Project menu, and can be accessed by selecting Project Properties.
The Make tab of the Project Properties dialog box allows you to add information to an executable. By adding code to your project, you can retrieve the property values of the application object. Other applications can also retrieve this information with API calls. Figure 11-1 illustrates the Make tab of the Project Properties dialog box.
Figure 1: The Make tab of the Project Properties dialog box
The first section of the Make tab allows you to set the version number of your application. You can set Major, Minor, and Revision release numbers from zero to 9,999. Checking the Auto Increment check box will increase the revision number by one, each time you compile your project with the Make Project command on the File menu.
Additional version information can be placed in the Version Information section. By scrolling through the Type list, you can choose different kinds of information and then type the value for the particular type in the Value area. The types of information available to you are Comments, Company Name, File Description, Legal Copyright, Legal Trademarks, and Product Name.
On occasion you may write applications that require arguments. In such cases, a user has to type the executables name followed by arguments required by the application. For example, you might create a program called TIME.EXE that performs actions within a certain amount of time. In such a case, the user would start the application by using Run from the Windows Start menu, typing the executables name followed by the argument of how many minutes before the task starts. If he or she wanted the application to perform its activities in 60 minutes, the user would type: TIME.EXE 60.
However, in Design mode, you can only run an application by pressing F5 or by choosing Start from the Run menu. This doesnt give the opportunity to add arguments. In cases where arguments are required by an application, you can type them into the Command Line Arguments field. In doing so, VB will start as if the application were started from a command line with the arguments added. Any arguments placed in the Command Line Arguments field wont be compiled with the project. This allows a user to use his or her own arguments when starting the executable, without problems from Command Line Arguments that were used in Design mode.
The Conditional Compilation Arguments field on the Make tab will be covered in the next section of this chapter. Though important to compiling your application, the amount of information on conditional compilation requires a section of its own.
The Compile tab allows you to set how your application compiles, anddepending on the method you chooseallows you to set a number of options for optimization. The two choices offered to you in the Professional and Enterprise editions of VB are Compile to P-Code and Compile to Native Code.
P-code is short form for pseudo-code, which is an intermediate step between high-level instructions (the code you write in VB) and low-level instructions (the native code your computer executes). When you compile your project, your VB code is converted, and elements of the code is compiled into compressed tokens. These tokens are placed into the compiled executable. By compressing the VB language code into a compressed form, it is executed faster than if it were in the original format. In comparison to native code executables, programs compiled as p-code are generally smaller in size.
Compiling into native code skips over the intermediate step of p-code, and compiles it into native instructions used by the processor. This increases the speed of processing loops and mathematical calculations. When it is compiled, VB uses a compiler engine that Microsoft uses for C++ development suites. Because native code is the same format that C executables are compiled in, you can use debugging tools for C language executables on VB executables. There are numerous benefits to compiling in native code over p-code.
Choosing to compile in native code offers you a number of options for optimization, which are seen in Figure 11-2. As was mentioned at the beginning of this chapter, optimization is a trade-off, and this becomes apparent on the Compile tab. You can choose to optimize for speed, size, or neither. You cant choose both size and speed. You can only set the compiler to favor one over the other when it compiles your project.
Figure 2: The Compile tab of the Project Properties dialog box
The other two general optimizations on this screen are Favor Pentium Pro and Create Symbolic Debug Info. Favor Pentium Pro will cause your application to run faster on computers with Pentium Pro processors. If you select this, your application will still run on computers with other processors, but they will run a bit slower. Create Symbolic Debug Info will have the compiler create a debug file with the extension .PDB. This file can be used by external debugging tools to debug the application as it runs.
Also on this screen is the Advanced Optimizations button. By clicking this button, youll bring up the dialog box shown in Figure 11-3. This screen has a number of options that allow you to increase the speed or reduce the size of your executable. However, they are in a separate area from the other options for a reason. As the warning on the dialog box says, depending on how youve written your code, setting these options can cause serious problems.
Figure 3. Advanced Optimizations
Exam Watch: It is important to know the optimization options on the Project Properties tabs and the effect that each will have on compiling. Many questions on the exam deal with problems resulting from changing settings; knowing the settings and effects can lead you to the correct answer.
An alias is a name that refers to an area of memory thats already referred to by another name. The Assume No Aliasing check box tells the compiler that the application doesnt use aliasing. When you check this box, the compiler can then optimize your code in ways that it wouldnt normally be able to do, and performance will increase. However, if you have included any code in your project that passes arguments by reference (which is the default argument passing style in VB), problems will result when the application is run.
In an array, there is a range of indices that can be accessed. For example, if you have an array of 100 indices, and you try and access the hundred and first index, an error message is displayed. This is because VB adds code to your application that checks the array bounds to determine that the index accessed isnt out of bounds. By selecting Remove Array Bounds Checks the compiler doesnt add this code, and your application will run faster. However, if the program reads and writes outside of the array, it will be reading and writing to unrelated memory areas. Not only can this cause problems for your application, but to programs using other memory areas as well.
The next two options on the Advanced Optimizations screen are similar. Remove Integer Overflow Checks determines whether a value is within the range of Byte, Data, and Integer data types. Remove Floating Point Error Checks does the same thing, but with Single and Double data types. The compiler normally adds these checks to your application and will generate a runtime error if the value is greater or less than the data type is meant to contain. Selecting this box will speed up calculations, but can produce unexpected results if the data type is overflowed. Selecting these options turns off the error checking, while deselecting them enables the error checks.
Checking the Allow Unrounded Floating Point Operations check box will cause your application to use floating point registers more effectively and avoid loads and stores from memory. In addition, it will also increase the precision of floating point comparisons. Sounds great, except that it can cause some unexpected errors. Because the comparison of floating point is more precise, it can cause the comparison of floating point numbers to be found unequal when you expect them to be equal! This can cause your code to react unexpectedly, or provide results that you deem incorrect.
You may remember that when Pentium chips first came out there was a big stink shortly afterward about bad chips. In 1994, Intel admitted to a flaw in the processor. When floating-point algorithms are used in an application, and a certain combination of digits are divided by each other, the answer comes up as flawed. The problem only seems to affect arithmetic division in math-intensive programs. Intel claims this problem will only crop up once every 27,000 years, while IBMs claims are more conservative: once every 24 days. No matter which company you believe, owners of these Pentium chips are able to replace the chips by contacting Intel. You also should consider that unless your application uses math extensively, the Pentium problem shouldnt affect it.
Because the problem exists, your compiler adds code that will produce the correct answer, even if the processor running the application is flawed. Because this code is added, your applications performance will decrease, as extra work needs to be performed by the code to check answers. When you consider the points in the above paragraph, it becomes apparent that it is fairly safe to enable the Remove Safe Pentium FDIV Checks option. Checking this option will remove the added code and make your application run faster. However, if your application is math-intensive (such as a science or engineering program that does intensive math), you should leave this option unchecked.
On the Job: While the Advanced Optimizations options allow you to set numerous settings for optimizing a program, you should only use them if youve considered the implications of each setting. If you configure these settings and experience problems, try removing the settings before altering your code. The optimization settings in Advanced Optimizations may be causing your problem, and its easier to test that (by clicking on a check box) than to rewrite code.
There may be times where you dont want certain code in certain compilations of your application. For example, you may have different versions of a program with different features, or applications that are distributed to different countries. Depending on the countries to which youre distributing your application, you may need to change the language or currency symbol. When your requirements certain code be included or removed from an application, conditional compiling can be done.
Conditional compilation allows you to create one project that deals with all versions of your application. Compiler constants are created to indicate version information about code to be used, and compiler directives are used to specify what code is to be added to a project. The way you can differentiate compiler constants and directives from other code is by preceding them the pound sign (#).
Compiler constants allow you to specify which version is to be used during compilation. You can almost think of a compiler constant as a title. You define the constant in your code with #CONST and give it a True or False value. When the compiler works through the code, it looks at whether the constant is True or False, then compiles the code related to it. Examples of #CONST are as follows:
#CONST conEnglish=True
#CONST conSpanish=False
#CONST conEnglish=-1
#CONST conSpanish=0
The values of True and 1 are synonymous, as are False and zero. You can use either in your code when defining such constants.
Conditional directives outline what is to be added and ignored during compilation. If the value of the compiler constant evaluated to True, then that section of code would be compiled. Any constant evaluating to False will be ignored. For example:
#If conStandardVersion Then
Insert code specific to Standard version here
#ElseIf conProfessionalVersion Then
Insert code specific to Professional version here
#Else
Insert code for other versions here
#End If
The scope of compiler constants is Private, and cannot be made Global in your code. However, when you specify compiler constants in the Make tab of the Project Properties dialog box, they are global. Specifying constants on the Make tab is done in the Conditional Compilation Arguments field. In this field you can enter assignment expressions, such as:
conEnglish=-1
Or
conEnglish=1;conSpanish=0
As you can see in the second line of the above code, you can enter multiple assignments by separating them with a semicolon.
It is important to remember that any compiler directives and constants you add to your project wont be compiled into the application. They are instructions for the compiler, and not part of the actual executable. As such, you neednt worry about the size of directives effecting performance.
Exercise 11-2: Conditionally Compiling an Application
#Const conOne = False
#Const conTwo = False
#If conOne Then
MsgBox "First conditional option was used"
#ElseIf conTwo Then
MsgBox "Second conditional option was used"
#Else
MsgBox "Welcome!"
#End If
The Debugging windows allow you to monitor whats happening to data in your code. You can see what the value of variables and expressions are, and enables you to determine if logic errors are present. Logic errors occur when your code returns a result that wasnt expected, such as if you expected sales tax to be added but your program is subtracting it. Though the code is running properly, the results are not what you wanted. By using the Debugging windows in VB, you can monitor the values of expressions and variables.
The Debugging windows consist of three separate windows: the Immediate, Watch, and Locals windows. The Locals window allows you to monitor the value of any variables within the current procedures scope. The Immediate window lets you type code that responds as if it were directly in your code. Through this window, you can also view the results of debugging statements in your code. The Watch window allows you to specify which expressions to watch and returns information about their values as your program runs. Together, they are essential tools for debugging your application.
Before going in-depth into each of these windows, it is important that you understand the Debug toolbar. When using any of these three windows, the Debug toolbar (Figure 11-4) is used to step through statements of code and bring up the Locals, Watch, and Intermediate windows. To view the Debug toolbar, click on View, select Toolbars, and then click on Debug.
Figure 4: The Debug toolbar
The Debug toolbar has a number of options that save you from having to constantly access the Debug, Run, and View menus. By using the Debug toolbar, you can start, stop, and pause execution, as well as use functions to step through code. The different buttons are listed below in Table 11-1, and detail each of the toolbars buttons from left to right.
Button |
Other Methods to Access this Function |
Description |
Start/Continue | F5, or clicking Start item on the Run menu | Starts an application. This button becomes the continue button if the application is in Break mode. |
Break | Ctrl-Break, or clicking Break item on the Run menu | Temporarily stops execution of the program. |
End | Clicking End item on the Run menu | Stops running the application, and returns the user from runtime to Design mode. |
Toggle Breakpoint | F9, or by clicking the Toggle Breakpoint item on the Debug menu | On the current line of your code, clicking this will set up or remove a breakpoint. A breakpoint signals that execution should stop on that line. |
Step Into | F8, or by clicking Step Into item on the Debug menu | Clicking this will execute your code one line at a time. |
Step Over | Shift-F8, or by clicking Step Over item on the Debug menu | Clicking this will execute code one line at a time, but when it reaches a call to another procedure, it will execute that procedure as one step and move to the next line. |
Step Out | Ctrl-Shift-F8, or by clicking Step Out on the Debug menu. Only available in Break mode | Clicking this will execute code one line at a time. If the line is a call to another procedure, the next line displayed is the first line of the called procedure. |
Locals Window | Click Locals Window item on the View menu | Displays Locals window. |
Immediate Window | Ctrl-G, or by clicking Immediate Window item on the View menu | Displays Immediate window. |
Watch Window | Click Watch Window item on the View menu | Displays Watch window. |
Quick Watch | Shift-F9, or by clicking the Quick Watch item on the Debug menu | Displays Quick Watch dialog box. The value displayed will be the current value of the expression youve selected. |
Call Stack | Ctrl-L or by clicking Call Stack item on the View menu. Only available in Break mode | Displays Call Stack dialog box. This dialog box shows called procedures that are called by the current procedure, but have not yet completed. |
Table 1: Buttons on the Debug Toolbar
The Watch window allows you to monitor the information dealing with watch expressions, which are expressions (variables, properties, and so on) that youve decided to watch during your applications execution. You can specify which expressions to monitor, and then view their values as the program runs.
The Watch window (illustrated in Figure 11-5) is made up of four columns. The Expression column shows the name of the expression being monitored. These expressions can be variables, arrays, properties, and so on. The Value column shows the current value of the expression, and the Type column shows what data type the expression is. Finally, the Context column shows the context of the current expression, such as what procedure the expression is associated with.
Figure 5: The Watch window
The Locals window shows the current value of all values that are declared in the current procedure, when your application enters Break mode. You can specify where in the code you would like Break mode to occur, by clicking on the desired part of code, and then clicking the Toggle Breakpoint button on the Debug toolbar. When execution reaches this part of the code, it will automatically enter Break mode. You can also click the Break button on the Debug toolbar, though this wont be a precise method of entering Break mode.
The Locals window (Figure 11-6) is made up of three columns. The first column shows the name of the expression in the scope of the current procedure. All declared variables are shown here, as are object variables. The Me expression appears in the Locals window, showing the current form. The Value column shows the current value of an expression, and the Type column shows the expressions data type. If the expression is an object, the Type column will display what object it is.
Figure 6: The Locals window
As with the Watch window, any structured variables (such as arrays or object variables) will have a plus sign (+) beside the expressions name. By clicking on the plus sign, you can view additional information dealing with that expression. For example, if you had an array named intNum, you could view the values of each element in the array by clicking the plus sign. To collapse this expanded view of the expression, click on the minus sign (-) that appears when the expression is expanded.
The Immediate window, illustrated in Figure 11-7, allows you to view the current value of an expression at runtime, and allows you to type code during runtime as you would during Design mode. If you wanted to test a line of code youd just come up with during runtime, you can use the Immediate window to see if it will work. In addition, you can print the values of expressions in the Immediate window to view what their current values are.
Figure 7: The Immediate window
To use the Immediate window, your application must be in Break mode. Once in Break mode, you can then type code into it as if it were the Code window used during Design mode. Any code you then type will immediately execute on pressing Enter. This of course assumes that your cursor is on the line of code you want to execute when you press Enter.
The Print method allows you to view the values of variables in the Immediate window. There are two variations of the Print method, which are quite similar. With the first method, type the word Print followed by the variable you want to view the value of. The second method is a shorthand version of the Print method. By typing a question mark (?) followed by the variable name, you can view the current value. Each of these are shown in the following examples:
Print intNum
? intNum
You can also do "What if" scenarios on the values displayed with the Print method. For example, if you wanted to view what a percentage of the value of intNum might be, you could type:
Print intNum * .25
or
? intNum * .25
After pressing Enter, the value of the What If scenario would be shown in the Immediate window. Performing such an action wont affect the results of the original variable, as it doesnt affect the variables actual value.
If you wish to change the actual value of a variable with the Immediate window, you can do it as you would in your actual code. This is done by assigning a new value to the variable, as shown in this example:
counter = 50
By assigning the value of 50 to the variable called counter, your code will now execute the remaining code of a procedure with this value. As far as your program is concerned, any new values assigned to a variable in the Immediate window is the same as if it were assigned through the Code window.
Because any of the above methods shown above require you to be in Break mode, this can cause problems when viewing large amounts of data. As in the case of large arrays, you must step through your code and print each current value. To help you in cases like this, you can use Debug.Print in your code to view current values as the program runs.
In the following example, a counter has been created that loops from one to 100. By placing Debug.Print into the code, we can view the value of a variable as execution moves through each loop:
Dim counter as Integer
For counter = 1 to 100
Debug.Print counter
Next counter
When this code is run, you can view the value of the variable counter in the Immediate window.
Unlike Print and ?, Debug.Print wont work when typed into the Immediate window. To use Debug.Print, you must type it directly into your code. Likewise, Print and ? are only used in the Immediate window, and not typed directly into your code.
Exercise 11-3: Using the Immediate Window to Check and Change Values
Open the project begun in Exercise 11-1. On the View menu, select Toolbars, and then click Debug. The Debug toolbar will appear.
Click the Immediate window button on the Debug Toolbar, so that the Immediate window appears.
In the Click event of the command button on Form2, add the following statement on the line immediately before the line that says "Next Counter":
Debug.Print counter
Run the program by pressing F5. Click Form2s command button, and notice that the value of the counter variable displays in the Immediate window.
Stop the application. In the Code window, go back to the Click event of Form2s command button. Click over the line that says "For counter = 1 To 100", so that the cursor is now on that line. Click the Toggle Breakpoint button on the Debug toolbar. Notice that this line now appears highlighted with a breakpoint.
Run the program again. Click Form2s command button, and notice that execution breaks at the loop. Click the Step Into button on the Debug toolbar to step through the loop several times.
In the Immediate window, type Print counter, and press Enter. The value of counter appears in the Immediate window.
In the Immediate window, type:
counter=50
Print counter
The value of counter is now 50, and stepping through the code will show that this value affects the remainder of the loop. Counter will go from 50 to 51 to 52 and so on.
Stop the program and save it. We will use this project in future exercises.
Setting a watch expression in Design mode is done through the Debug menu, by selecting Add Watch. Any expression that youve placed your cursor over in the Code window will appear in the Expression text box of the Add Watch dialog box, illustrated in Figure 11-8 below. If you decide that youd like to watch a different expression, you can type in the name of the expression to watch in this field. The Procedure listbox has a listing of all procedures in the current form or module, while the Module listbox has a listing of all forms and modules in your project.
Figure 8: The Add Watch dialog box
The Add Watch dialog box also allows you to specify what is to occur when execution reaches the expression. Watch Expression will simply monitor the expression. Break When Value Is True will send the application into Break mode if the value of the watched expression has a value of True. Break When Value Changes will send the application into Break mode if the expressions value changes at all. What kind of problem you are debugging will determine which setting you use.
You can also set watch expressions with the Quick Watch button on the Debug toolbar. The Quick Watch button only becomes active during runtime in Break mode. You can click the Break button or set a breakpoint in your code to enable this button. During Design mode, Quick Watch is available from either the Debug menu or from the Debug toolbar.
By putting your cursor over a variable, you can add the expression to the Watch window with Quick Watch. Unlike the Add Watch command, you cannot edit the expression chosen and select a different variable or property from another module or procedure.
Once youve added a watch expression, you can then view its value in the Watch window as the program runs. As seen back in Figure 11-5, the Watch window has four columns including the Value column. It is in the Value column that an expression's value appears. To change the value of an expression, you just click the value and change it. After moving off of the new value (by pressing Enter, Tab, or clicking elsewhere) the new value is assigned to the expression.
The Edit Watch item on the Debug toolbar allows you to modify and delete watch expressions. The Edit Watch dialog box is exactly the same as the Add Watch dialog box, except that it has a Delete button added. By selecting an expression from the Watch window, and then choosing Edit Watch, you can modify or delete that entry.
Exercise 11-4: Adding Watch Expressions
When monitoring watch expressions, it is important to be aware of the scope of a variable. The scope of a variable is the area that other parts of code can be aware of an object or variable. For example, if you declare a variable called intCount in a procedure, only code within that procedure can access and modify the value of intCount. Its scope is procedure-level, and only code within that scope can use the variable. As such you need to be aware of the scope of the variable youre watching.
Depending on how a variable is declared, it can have a procedure-level and module-level scope. Procedure-level variables are available only to code within that procedure. Variables that are module-level can be declared as Private or Public. Private declarations are available to the module that they appear in, while public declarations are available to all modules.
When using variables that are out of scope (not available to the currently running code), the Watch window will show it. The Value column will show <Out of Context>, meaning that the expression being monitored is out of scope. If the variable is needed to be monitored at all times, you should define a different scope for the variable. For example, if the variable was declared at a procedure-level, you should return to Design mode and declare the variable as Private or Public.
The Locals window allows you to check and change the values of expressions in the current procedure. It also shows the Me variable, which is the current form. These values only appear when the application is in Break mode.
If you wish to change the value of an expression through the Locals window, click the value of an expression, which is listed under the Value column. You can then change the value, and by moving off of the value, it will retain its new value. You can move off of the value by pressing Enter, using navigation keys, pressing Tab, or clicking off the value with your mouse.
Exercise 11-5: Checking and Changing Values in the Locals Window
Until this point, youve been working with only one project at a time. VB allows you to work on multiple projects at once, called project groups. By working with project groups, you can increase your productivity by having several projects loaded at once to work on.
You can also use project groups to test your code in a test project, before adding it to the application you plan to distribute. One project is used to test your code, while the other is used as your "real" application. In this way, you dont need to worry about ruining a project by adding code that youre uncertain will work.
To create a project group, you need to add a project to your current project. From the File menu, choose Add Project. You can then add a new project or an existing one. You can select from Standard EXEs, ActiveX EXEs, and so forth, just as you can when creating a new project.
When a project is added, you have created a project group. The Project window reflects this by stating in the title bar Project Group, followed by the Group name (that is, Group 1). You can move between projects in the group by double-clicking the forms or projects contained in the group.
Since multiple projects now exist as a group, you must designate one of these projects as the startup project. The startup project is the project that will start when you press F5, or use the Run menus Start command. To set a specific project as the startup project, select the project in the Properties window, then right-click to bring up the contextual menu. From this menu, select Set as Start Up. The startup project will appear bolded, showing that this is the designated startup project. All other projects will appear in normal text.
The Professional and Enterprise editions of VB can be used to create project groups for creating and debugging applications that use ActiveX components. By using project groups, you can create and debug groups that contain ActiveX dynamic-link library projects, ActiveX control projects, ActiveX EXEs, or standard EXE projects.
To change an existing project to any of these ActiveX projects, you must use the General tab of the Project Properties dialog box (which is accessed from the Project menu). On the General tab, use the Project Type listbox. This listbox allows you to select what type of project the currently selected project will be. You can choose from Standard EXE, ActiveX EXE, ActiveX Control, or ActiveX DLL. This allows you to convert a project after it has been created with the Add Project menu item.
By using project groups, you can load in-process components with your standard executable application and run all the projects together. This allows you to step from the applications code into the in-process components code. To do this, you must create a project group consisting of your standard executable, and whatever in-process components you wish to include.
Once youve included your ActiveX DLL to a project group, you can then go about the process of debugging the DLL. It is important to remember that the process of debugging ActiveX DLLs is the same as debugging most other in-process components.
The first step to debugging an ActiveX DLL is setting breakpoints and watch expressions, as shown in the previous sections. After adding breakpoints and watch expressions in the class module and control code, you are then ready to fully compile the project.
To debug an ActiveX DLL (or most other ActiveX components), you should fully compile the project. The reason is because ActiveX components wont unload while its running in the development environment. You can have all references to it released and meet all of the shutdown conditions, but it will still remain in memory. Therefore, to test the unloading of a DLL, you must fully compile components.
The default behavior of VB version 6 is to compile on demand. This means that some code isnt compiled until a client calls for it. As compiling errors often require fixing the error in Design mode, this means you would have to return the entire program group to Design mode to fix the error.
To resolve compiling errors, you should fully compile your project or remove the compile on demand setting. To run the project after fully compiling it, start your project by choosing Start with Full Compile from the Run menu, or by pressing Ctrl-F5. You can disable the Compile On Demand setting from the General tab of Options, which is accessed from the Tools menu. On the General tab, uncheck the Compile On Demand check box. When your program now runs, everything will be compiled before starting.
After running your debugging session, it is important to shut down your application properly. This means using the Close button on your applications main window to return to Design mode. The reason for this is that using the End button on the Debug toolbar will cause every project to close down. Your DLL will not receive Terminate events from the application, so you wont be able to effectively test how it shuts down. To test the shutdown behavior of a DLL effectively, you must always use the Close button.
To debug and test a control in process, you should follow the procedures outlined in the section above. First create a project group by adding your ActiveX controls to the initial project. You must then add the necessary breakpoints and watch expressions to your control, and you should fully compile the project before testing the code. Fully compiling the project will allow you to deal with compiling errors before dealing with problems (such as logic errors) that exist in the control itself. Once this is done, youre ready to deal with the issues that affect controls.
Debugging controls are different from debugging other objects. Why? Because parts of your code in your control has to execute while the form (that the control instance is placed on) is in Design mode. If the code isnt able to run, then you will be unable to access such things as property procedures and resizing.
There are several areas of control code that you must consider when debugging a control. The first is the property procedures that are used to implement your controls properties. These allow you to set the controls properties using the Properties window. The next is the code that saves and retrieves property values. When you load a form containing a control instance, run the project, or save it to disk, this code has to be able to run at Design mode. Finally, the Resize event must run in Design mode so that you have an appearance of the control on your form. If the control is user-drawn, then the Paint event must be run.
The way that you allow such code to run at Design mode is closing the controls visual designer. This is done by clicking the visual designers Close box, or by using Ctrl-F4. One the visual designer is closed, the icon representing the control appears in the Toolbox, so you can add instances of the control to your forms.
When the controls visual designer is open all instances of the control are disabled. The icon for the control, which appears in the tool bar, is disabled and appears grayed-out. Should you need to open a form that contains instances of a control whose visual designer is open, the control will be covered with cross-hatching.
Since your control is running when the visual designer is closed breakpoints encountered in the control during Design mode will engage, as if the program as a whole were running. When this occurs, Break mode is entered. You can either click the Continue button on the Debug toolbar, or press F5 to continue if this occurs. This avoids having to remove the breakpoint while using the control in Design mode.
There are two ways to deploy a VB application: manually, or with the Package and Deployment Wizard. The Package and Deployment Wizard is covered in the following sections. In this section we will cover manual deployment.
Manual deployment takes a little more thinking than the wizard but its not very difficult. Before packaging and deploying your application, you must first compile it. From the File menu, choose the Make command. This command is followed by the name of your project, so that if your application were named Project1, it will say Make Project1.exe. A dialog box will then appear, allowing you to choose where the executable will be compiled. After clicking the OK button, the project will compile and become an EXE file.
If your project is part of a project group, you must first select which project in the group you want to compile. In the Project window, select the project you want to compile, and then select the Make command from the File menu. If you would like to compile the entire project group, select Make Project Group from the File menu. A dialog box will appear. After clicking the Build button, the group will be compiled.
Once the project is compiled, youre ready to copy the files onto the distribution media you intend to use. After that, its just a matter of uploading it to the site. If youre using other deployment methods, then the procedures are a little more involved.
Using floppy disks requires that you put the files for your application in a certain order. You must put the SETUP.EXE and SETUP.1ST on the first disk, then all files that are listed in the Bootstrap section of the SETUP.1ST file. To view this file, you can use any text editor. Any other CAB files are then placed on additional disks.
To distribute your application to a network share, you must first ensure that you have the property permissions or rights (depending on the network operating system used) to access the share. You then use the command prompt or Windows Explorer to access the share and copy the setup files to the target directory.
CD-ROM distribution requires that you have a CD-ROM burner. You wont get far without one. You then copy your files to the CD.
Doing it manually is like a barber not using a bowl when cutting Bill Gates hair you can do it, but why bother with the extra work? The Package and Deployment Wizard is a much easier solution. The wizard walks you through packaging and deploying your application with screens that explain every step. There is no need to follow any of the steps in the previous section if you intend to use the wizard. Figure 11-9 shows the main screen of the Package and Deployment Wizard.
Figure 9. The Package and Deployment Wizard's main screen
The Package and Deployment Wizard is actually two wizards and one dialog box, which you can access from the main screen. The Package Wizard allows you to package your program into CAB files or a self-extracting executable. The Deploy Wizard allows you send your package to a distribution site or media, such as an Internet site, floppy disk, and so forth. The Manage Scripts button opens a dialog box that allows you to rename, delete, or duplicate package and deployment scripts. Such scripts can be used to manipulate the packaging and deployment of an application.
Before using the wizard, you should compile the application. To compile your application, you can follow the steps provided in the previous section. However, upon clicking the Package button, youll be informed if your application hasnt been compiled yet. The resulting screen will offer you the option of compiling the project from here. After clicking the Compile button, the wizard will automatically compile the project, then move on to the Packaging Wizard.
To package an application you must first choose what saved project you want to bundle. This is done from the Package and Deployment Wizards main menu. The Select Project listbox contains a listing of projects that were previously used in the wizard. If you want to bundle a new project, select the Browse button and select a saved project from your hard disk.
After selecting the project you wish to use, click the Package button. Your project will be analyzed, and the Package Wizard will be started. The Package Type screen will appear, asking you to select the type of package you want to create. The first option is Standard Setup Package, which will create a package that is installed by a SETUP.EXE file. The other option will create a file listing information about runtime components that are required by your application to run. To create a setup program, select Standard Setup Package and click Next.
The following screen asks you to specify where you would like the package to be created. You can have the package assembled in an existing local folder or a network folder, or you can use the New Folder button to create a new folder for the assembly. If you accept the default location, a folder called Package will be created in the folder that contains your project. Clicking Next will bring up a message box that asks you to confirm creation of the Package folder. Click OK to continue.
The Included Files screen lists all files that the Wizard determines are needed by your application to run. It determines this information when the Package Wizard starts and analyzes the project. In almost every case (I say "almost" to cover my butt. Ive never seen an incorrect listing result.), the listing will be correct. If you require some files not to be included with the package, deselect the check box beside the name of the file. If you want a file added, click the Add button on this screen. An Open File dialog box will appear, allowing you to browse your hard disk for files. You may need to add a file to your project in cases such as when youre packaging a database program, or when third-party controls are used in your application. As an example, if your application used an Access database file, you would need to add this file to your package.
The next screen youll encounter will be Cab Options. This screen allows you to decide whether you want the package to have one huge CAB file, or multiple CAB files. You might select Single Cab if you were distributing your application on CD-ROM, a network folder, or over the Internet. However, if you are going to distribute the application on floppy disk, you must choose Multiple Cabs. Upon selecting Multiple Cabs, a listbox for Cab Size will become enabled. You can use this Cab Size list to specify whether youd like the cab size to be 1.44MB, 2.88MB, 1.2MB, or 720KB. The cab size you choose should correspond to the size of the floppy disks youre using for the package.
Exam Watch: While you probably wont have to deal with a question that requires knowledge of every CAB size available, you should know that multiple CABs must be used for floppy distributions. To use a single CAB wont enable you to fit the application on one disk.
The next screen is the Installation Title screen, which allows you to specify the title of the package. This name will be displayed when the application is installed. By default, the name that appears in the Installation Title field is the name of the project. If this isn't suitable, simply type in the new name youd like to be displayed at installation.
The screen that follows (illustrated in Figure 11-10) allows you to specify what will appear in the Start menu, when the user installs your application. This screen offers you a Windows Explorerstyle interface, which shows a tree of where on the Start menu your application will be placed. You can change the name and properties of your menu group or item by clicking the Properties button. This button will display the name of the menu item. If it is a group item, you can change whether it is a Private or Common group. If it is a menu item, you can also change the target (application that the item represents) and where the application will start in. You can choose the application path, system path, and numerous other places to start. The New Group button allows you to add additional groups to the Start menu. New Item opens a dialog box that is identical to the one that appears when you choose Properties for a menu item, and adds a new item to the menu. If you want to remove any groups or items, click the Remove button.
Figure 10: The Start Menu Items screen of the Package Wizard
The next screen is Install Locations, which lists certain files (like your applications executable) for which you can modify the installation location. At the end of the listed file, there is a column called Install Location. Clicking an item in this column will display a drop-down list with a number of choices for installation. You can choose to install into the application path, system path, or numerous other choices.
Following this, you will see the Shared Files screen. The wizard displays a listing of files that can be shared by other applications. At the very least, it will display the name of your applications executable. If you want a file in this listing to be shared, you should select the check box beside the files name.
The final screen allows you to specify the name of the script for this session. This will contain the settings you choose for your session with the wizard. After naming this script, you can later duplicate, rename, or delete it by clicking the Manage Scripts button on the Package and Deployment Wizards main screen. If you dont want to save the settings, simply accept the default name and click the Finish button. Clicking the Finish button will start the bundling of your application based on the settings you choose using the wizard.
When you deploy an application, you must include all of the COM components you use in your application. ActiveX controls (OCX files) and ActiveX DLLs have to be included with the package. These are listed on the Included Files of the Package Wizard, and you should verify that all of these have been included.
If a user installs your application, and a COM component cant be found, the VB Runtime Library will produce a "File not found" message. You should have all of your OCX files installed into the Windows\System directory. If an OCX doesnt have this path, you should change this in the Install Location column of the Install Locations screen.
If you are using COM components that are included with VB version 6, you wont need to worry about registering them with the Windows Registry. This is because these files are self-registering. If you want to use an ActiveX component that youve created, you should create a dependency file so that the component can be added to the SETUP.1ST file and then registered upon installation.
Dependency files have the extension of .DEP, and contain information about runtime requirements of an application or component. This information includes such things as the files needed by the component or application, where these files should be installed, and how they should be registered with the Registry. When you click the Package button in the Package and Deployment Wizard, it scans for dependency information. It reads the information, builds a list of files required, and builds a listing of installation information. Dependency files are read, and the information is added to a SETUP.1ST file that resides outside of a Standard Setup Packages SETUP.EXE file. Internet packages have this file written to an INF file stored in the CAB.
After clicking the Package button, the Package portion of the wizard starts. As was previously mentioned, you can then choose to create a standard setup package or a dependency file. After selecting Dependency File and clicking the Next button, you are presented with the Package Folder screen. This allows you to specify where the package will be assembled. For further information on this screen, see the previous section. Clicking Next, you reach the Included Files screen.
Included Files allows you to specify what files are to be included with the package. The list contains all dependent files that the wizard has determined are necessary from analyzing your project. Clicking the Add button can include any additional files you want to add. Upon doing so, a dialog box appears that allows you to browse your hard disk. After specifying the files to add, click Next.
The next screen is only important if you are bundling the dependency file into a CAB file for dependency on the Internet. Here you can enter the name for the CAB file, a URL from which it can be retrieved, and the default file to execute when the file is downloaded. The file to execute can be either an executable or an INF file. If you are not packaging the dependency file for the Web, accept the defaults, and choose next.
The screen after this is the Install Locations screen. This screen allows you to specify where files are to be installed. As mentioned in the previous section, you can modify the location through the final column on this screen. The Install Location column allows you to change the installation location of a particular file, via a drop-down list.
The final screen allows you to name a script for the dependency settings you specified in this setting. You can duplicate, rename, and delete this file by clicking the Modify Scripts button on the Package and Deployment Wizards main screen.
The GetSetting and SaveSetting functions allow you to read and write information to and from the Windows Registry, respectively. These are built-in VB procedures that access entries under the HKEY_CURRENT_USER\Software\VB and VBA Program Settings. You can use these functions to programmatically manipulate the Windows Registry.
GetSetting and SaveSetting both have the following arguments: appname, section, and key. The appname argument is the applications name, section is the name of the section where a key setting is found, and key is the name of the key to return. SaveSetting also has a fourth argument called value, which allows you to specify the contents to be stored in the Registry.
If you wanted to save settings to the Registry, you could do something like this example:
SaveSetting app.exename, strUser, strID, txtUser.Text
In doing so, the SaveSetting function would take the name of the currently running program, and the values of the other variables and save them to the Registry. To retrieve this information, you could use the following code:
txtRegInfo.Text=GetSetting (app.exename, strUser, strID)
Unlike most of the other material in this chapter, you would use the GetSetting and SaveSetting functions in your code. In doing so, you could access and write to the Registry and manipulate data contained therein.
While no programmer likes to admit it, there are times where users wont want to use your application anymore. Despite all your hard work, theyll want to cast your application by the wayside and uninstall it from their system. I know youll read this with tears in your eyes, but its important to know how to provide a user with the ability to uninstall your program.
Users can use the Add/Remove Programs applet in Control Panel. However, the Package and Deployment Wizard installs a program that works with Add/Remove Programs, so that the user can uninstall an application. While packaging your program, it adds a file called ST6UNST.EXE, which is the application removal utility. It installs this to the \Windows or \WinNT directory.
In addition to this, when the user installs your application, a file called ST6UNST.LOG is created in the application directory. This file contains information on directories that were created during installation, Registry entries that were created and modified, and self-registered DLL, EXE, or OCX files. In addition, the log file has information on links and Start menu items that were created and the files (and their location) that were installed. The listing of files is comprehensive. It lists all files in the setup program, including ones that werent installed because newer or identical files existed. It also specifies whether files are shared files, and (if shared) if setup replaced the existing file.
ST6UNST.EXE reads the log file and determines all files that should be removed. When executed, it will remove all files, directories, and other items logged in the ST6UNST.LOG. It allows the user to fully remove your application from their system.
In cases where youre selling your application, the most common methods of deploying an application will be on compact disc (CD-ROM deployment) or on floppy disks. The Package and Deployment Wizard allows you to deploy to such media easily. By following the screens, you should have little problem distributing an application on such media.
On the Job: It is important to offer both CD and floppy installations of your applications. CD deployment is better, as the user wont have to keep switching disks during the installation. However, remember that not everyone has a CD-ROM drive. Though an application may have to be split up over several floppies, it is wise to offer both installations. Not doing so will exclude a significant number of people from using your application.
Floppy disk deployment requires that you package your application into multiple CAB files. As shown in Figure 11-11, the Cab Options screen allows you to choose to bundle your application in a large single CAB file, or multiple CABs. You can select different sizes of CAB sizes that will apply to your CAB files. These sizes range from 720KB to 2.88MB. The size you choose will depend on the size of blank disks you are using to distribute the application. You cant use disks of different sizes or choose to deploy to more than one size of disk. Whatever size you choose will apply to all of the blank disks you use.
Figure 11: Cab Options screen of the Package portion of the Package and Deployment Wizard
Once you have properly packaged your application, youre ready to deploy your application to floppy disks. After selecting the package you want to deploy, from the Select Package listbox on the Package and Deployment Wizards main screen, click the Deploy button. The Wizard will analyze the project, and you will see a screen that asks you which script you want to use. Select the package you wish to deploy, from the listbox of scripts available, then click the Next button.
The Deployment Method screen asks you what method you wish to use to deploy your application. On the list of available deployment methods, there should be an entry for floppy disks. If you havent packaged the application as multiple CABs, the floppy disks option wont appear. If the floppy disks option doesnt appear, youll have to return to the Package Wizard and repackage the application as multiple CABs.
Exam Watch: Floppy disk deployment wont appear on the Deployment Method listing if you havent packaged your application as multiple CAB files. If this is the case, you must return to the package portion and repackage the application as multiple CABs.
After selecting floppy disks, click the Next button, and youll be asked which drive you want to use. If you have a single floppy drive, only one drive will appear in the drive box. You should also check the Format before copying check box. Floppy disk deployment requires blank, formatted disks. If you have existing data on a disk, deployment will fail. In addition, youve probably experienced buying a box of formatted floppies, only to find that the occasional one (or all of them) havent been formatted. To avoid problems, select this check box. After doing so, select the floppy drive to which you want to deploy, and click Next.
The final screen allows you to name the script used to save settings for your deployment session. As has been mentioned, you can rename, duplicate, or delete this script through the Manage Scripts portion of Package and Deployment Wizard. Clicking the Finish button on this screen will begin transferring files to the floppy disks.
Exercise 11-6: Packaging and Deploying an Application to Floppy Disk
The CD-ROM deployment of a desktop application is very similar to folder deployment, which is covered later in this chapter. In fact, you can use the folder deployment method for CD-ROM deployment. You would use Folder option, then transfer the files afterward to CDs. In most cases, this would be the option you would use in the real world. The reason for this, is that you will rarely encounter a CD burner attached to every programmers workstation.
A CD burner is also called a writeable CD-ROM. While normal CD-ROM drives only allow you to read from a CD, CD burners allow you to read and write to CDs. To write your packaged application to a writeable CD, you must insert a blank CD into your CD burner. While the price of writable CD-ROMs is still quite high, the price is dropping to a level that many people and smaller companies are able to afford.
Depending on the writeable CD-ROM you own, you may be able to deploy files directly to the CD with the Package and Deployment Wizard. This will allow you to transfer the files, with the settings youve chosen, directly to the writeable CD. For such a deployment, you can use either single or multiple CAB files, though a single CAB is more commonly used. The files are transferred to the CD with the same type of procedures as detailed in the network folder deployment.
The Internet is an effective way of deploying an application to users. Deploying a package via the web requires you to have some sort of Internet access and proper permissions to a directory on a Web site. Since youll be deploying your application to a directory on an Internet server, you must ensure that youre able to post (upload or transfer files) to a particular directory. Check with your Internet Service Provider or system administrator to determine if these permissions exist.
You can package your application as single or multiple CAB files, but single is often the better choice for Web deployment. Having too many files can often confuse novice users, and a single CAB file limits their choices as to what they need to download. After packaging your application, start the Deploy portion of the Package and Deployment Wizard.
On the Deployment Method screen of the Deployment Wizard, select Web Publishing, then click Next. The Items To Deploy screen lists all of the files in your package (SETUP.EXE, SETUP.1ST, and CAB files). If you dont want to deploy certain files, deselect the check box beside the files name. You will generally want to deploy all files listed. Click Next to move to the following screen.
You are given the option of selecting other files and folders to deploy. A tree is shown on this screen, displaying all files and directories in the directory containing your Package folder. Checking the checkbox beside a file or directorys name will include the file. This is useful if you are deploying a sample application, and want to include the code (that is, forms) for the application. It is also useful if youre deploying database applications and want to deploy a blank copy of the database. That way, if they experience problems, they can access a fresh database file.
Clicking the Next button will bring you to the Web Publishing Site screen. This screen allows you to enter the URL (uniform resource locator, or Web site address) of where you want the application deployed. You must enter a valid URL that you have permissions to upload to in the Destination URL combo box. If you leave this blank, you wont be able to continue. In addition, you have two protocols to choose from in the Web publishing protocol listbox: HTTP Post and FTP. You should check with the Internet servers system administrator to determine the preferred protocol.
Clicking the next button will bring you to the final screen. You can enter a name to save the settings for this session to a script, or just click Finish to complete the session. Your package will then be deployed to the Web site.
If your project contains an ActiveX control, you can deploy your project as an Internet package. This will create a CAB package that can be either downloaded from a Web site, or posted to a Web server. Using the Package and Deployment Wizard to deploy an ActiveX control is significantly easier than doing it manually.
From the main screen of Package and Deployment Wizard, select the project you want to package from the Select Package listbox, or click Browse to add your project to the list. Having done this, click Package. Your project will be analyzed. If you have created previous Internet packages, you can select a script to use from the listbox, or choose None to create a fresh installation package.
The next screen allows you to determine the type of package to create. Since the Wizard has determined that this project contains an ActiveX control, it offers the choice of creating an Internet package. You should select this package, then move through the following two screens. The two screens following this allow you to determine where the package is to be bundled and what files are to be included.
Next you will encounter is the File Source screen. These will list runtime CAB files that are linked to those on Microsofts Web site or another location, such as the CAB file itself. There is a listing of files that have such links. Clicking on each of the files will determine where the links reside. The Files Source section of this screen allows you to modify the source, so you can either include the information in the CAB file, download it from Microsoft, or download it from another source. Depending on the file, you may be limited on the options available in the Files Source section. For example, the OCX file for the ActiveX control you created may only be able to include the information or download it from a site other than Microsofts.
The next screen allows you to specify safety settings for your components. If certain components cant be determined as safe to initialize or script, they will be listed here. Files listed here will have a value of No for initialization and scripting safety. To set your component as Safe for Scripting and Safe for Initialization, click each of the columns for these settings and change their value to Yes.
Clicking Next will bring you to the final screen. You can rename an Internet package and save the settings for this session. Clicking Finish will create the package.
Clicking Deploy will analyze the project and bring of the first screen of the Deploy portion. The Package to Deploy listbox will list a new script for your Internet package. Select this script and click Next.
The next screen allows you to specify whether to use a deployment method of folder or Web publishing. Select Web Publishing and click Next to have your packaged published to a Web server. The screens that follow are the same as those covered in the previous section.
If you plan to deploy your application to a folder on a network, you must first ensure that you have the proper permissions or rights (depending on whether your network is NetWare or NT) to do so. In addition, the people youre planning to have use your application must also have proper permissions. If you or they do not, people wont be able to access the directory containing your application. Check with your network administrator to ensure that you and others can access the network directory.
After doing this, you are ready to use the Package and Deployment Wizard. After packaging your application as either a single or multiple CABs bundle, click the Deploy button to start the deployment portion of the Wizard. The first screen will display a listbox that allows you to select the package you want to deploy.
The screen that follows this is the Deployment Method (refer back to Figure 11-12). Select Folder from the list, and click Next to see the Folder screen. The Folder screen allows you to select a local or network folder for deployment. A Windows Explorerstyle tree is displayed, allowing you to navigate through folders on your hard disk. If you want to deploy locally, you could select a folder from the tree. To create a new folder, you would click the New Folder button, and then name the newly created folder.
Figure 12: The Folder screen of the Package and Deployment Wizard
Since were deploying to the network, the Network button on this screen would be used. This will display a dialog box that shows a tree of the network, which is similar to what you see when you use Network Neighborhood in Windows. Clicking through the listing, you would find the networked computer to which you wish to attach. You would then navigate to an existing folder ordepending on your permissionscreate a new folder for deployment.
Depending on the network youre on, and how your computer is configured, you may also be able to switch to the network drive using the Drive box. You could then navigate through the tree of folders to find the one you wish to deploy to. This would be the case if you were on a Novell NetWare network and had drives mapped to your computer.
Clicking the Next button will bring you to the final screen. Here, you can name the script that will contain the settings chosen in this session. Clicking the Finish button will initiate the deployment and transfer the files to the network folder.
Begin Q&A
How can I optimize an application for both size and speed? | You cant. Optimization is a trade-off, and you can only optimize for speed or for size, not both. |
How can I check for logic errors in my application? | Use the Debugging windows in VB. They will allow you to test your program and help you find and fix logic errors. |
How do I give a user the ability to fully uninstall my application? | The Package and Deployment Wizard automatically adds the file called ST6UNST.EXE to your package. This file reads the ST6UNST.LOG file created during installation and fully removes your application from their system. |
If a user already has the ST6UNST.EXE installed, does this mean they wont be able to uninstall my application when my installation tries to install it? | Each time one of your applications is installed, the setup program will put the ST6UNST.EXE on the users system. This wont effect an uninstall of your application, as the ST6UNST.EXE file reads uninstall information from the ST6UNST.LOG file thats created in the application directory. |
I am only using controls included with VB version 6. Do I need to create a dependency file for my package? | Generally, no. The Package and Deployment Wizard will analyze your project and include the files required for your application. |
Optimization allows you to improve the size or speed of an application. VB version 6 allows you to configure a number of optimization options through the Project Properties dialog box tabs. In addition, there are advanced optimization options you can set, which should only be configured if you understand what effects they will have on your application.
VBs Debugging windows allow you to monitor what is happening to data as you step through execution. The Debugging windows consist of the Watch window, the Local window, and the Immediate window. They are tools provided with VB to allow you to debug your application.
The Package and Deployment Wizard allows you to bundle an application into an installation package, then deploy it to a distribution site or media. By using the wizard, you are able to create professional installations quickly and easily. The wizard steps you through the process, allowing you to package and deploy an application by answering its questions.
Throughout the countless development cycles, an ongoing process of optimization should be undertaken. Application optimization should focus on one (two at most) areas of application characteristics: calculation speed, display speed, perceived speed, memory size, or executable size. Advances on one area often contradict desired progress in other areas, forcing either a balancing act of optimization procedures, or a distinct trade off. Its very important for the developer to properly recognize not only what to optimize, but where to optimize code for maximum return on development time.
Visual Basic also provides for conditional compilation directives for providing conditional logic during the compiling of an application executable. This greatly increases the flexibility of debugging and selective code inclusion during binary compiling.
In addition to application optimization, debugging plays a crucial role in application development. For this, be very aware of the part the Watch, Locals, and Immediate windows play in the debugging process. Knowing what options are available in each is crucial to understanding application debugging. Also note how project groups can assist in application development. Be prepared to describe the established methods for debugging DLLs, ActiveX controls, and using all the possible ways to invoke breakpoints for debugging standard executable projects.
After successfully wading through the mirage of development, optimization, and debugging cycles, application development remains. Be aware of the syntax for declaring conditional compilation directives, limitations on conditional compilation syntax structure, and how conditional compilation techniques can assist application debugging. For the exam, be able to explain the ramifications of choosing P-Code versus Native Code compilation, especially the effect of altering the Advanced Optimization choices available in Visual Basic.
To cover the bases, also be aware of the basics of application deployment methods, such as floppy, compact-disc, and web-based deployment, including the files generated.
By Michael Lane Thomas, MCSE+I, MCSD, MCT, A+
The following Self-Test questions will help you measure your understanding of the material presented in this chapter. Read all the choices carefully, as there may be more than one correct answer. Choose all correct answers for each question.