Section Start Page Next Page

Running Other Applications from Visual Basic, Creating Applications, Using DLLs, DoEvents/1

1.0 Running other applications

Top of page1.1 The Shell function
This Visual Basic function can be used to run any executable program from your Visual Basic application.

The syntax is:

integervariable = SHELL(commandstring [, windowstyle])

where

Constant Description
vbHide Window is hidden and focus is passed to the hidden window.
vbNormalFocus Window has focus and is restored to its original size and position.
vbMinimizedFocus Window is displayed as an icon with focus.
vbMaximizedFocus Window is maximized with focus.
vbNormalNoFocus Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus Window is displayed as an icon. The currently active window remains active.

Note The Shell function runs other programs asynchronously. That means that you cannot depend on a program started with Shell to be finished before the next Visual Basic statement is executed.

The following example demonstrates the Shell function:

Step 1 Start a new project and add two command buttons and a common dialog control to the form. Name the form as frmAppManager and set the caption of the form to Application Manager. Save the form as frmAppManager.frm and the project as FormManager.vbp

Form for shell function example
Figure 1-1

Step 2 Name the command buttons cmdCalculator and cmdOtherEXE and set their captions appropriately.
   
Step 3 Name the common dialog control cdlGetFile.
   
Step 4 Add the following code to the command buttons
Private Sub cmdCalculator_Click()
	Dim TaskID As Long
	TaskID = Shell("calc.exe", vbNormalFocus)
End Sub

Private Sub cmdAnyEXE_Click()
	Dim TaskID As Long
	cdlGetFile.Filter = "EXE File|*.exe"
	cdlGetFile.ShowOpen
	TaskID = Shell(cdlGetFile.filename, vbNormalFocus)
End Sub
Step 5 Save the files. Run the application and test the buttons. Select an EXE file such as Notepad.exe to test the RunAnyEXE button.

 

Top of page2.0 Creating a Windows application
You can freely distribute any application you create with Visual Basic to any Microsoft Windows user. Users will need copies of the following:

The following example shows you how to make an.exe file for a simple application. For complex applications, you should use the Setup Wizard to ensure that all necessary files are distributed to your user and installed correctly.

Step 6 Use the same project you currently have open (the Application Manager). Click on the form. In the properties window, set the Icon property for the opening form to a suitable icon. (There are lots of icons in subfolders of the Visual Basic Icons folder).
   
Step 7 Save the file as frmAppManager1.frm. Save the project as AppManager1.vbp. Add a command button named cmdCompanyName to the main form of your project. Set the caption to Company Name.

Application manager with company name button
Figure 2-1

This button will be used to retrieve version information from your application.

Step 8 Add the following code.
Private Sub cmdCompanyName_Click()
	MsgBox App.CompanyName,vbInformation
End Sub
Step 9 Select Make EXE File... from the File menu. You will be prompted for the location and name for the EXE file.

Make EXE dialog box
Figure 2-2

Step 10 Select options.

The options panel enables you to enter version information which will be stored in the EXE file. This information can be accessed from code using properties of the App object.

EXE options dialog box
Figure 2-3

Step 11 Enter some suitable data into the Company Name field.
   
Step 12 Click on OK.
   
Step 13 Save the .EXE file.
   
Step 14 Save the files and project. Close Visual Basic.
   
Step 15 Run the EXE file from the folder where you saved it.
   
Step 16 Click on the Company Name button to display the company information.

If you have the Professional or Enterprise Edition of Visual Basic 6.0, you can compile your code either in standard Visual Basic p-code format or in native code format. Native code compilation provides several options for optimizing and debugging that aren’t available with p-code. P-code, or pseudo code, is an intermediate step between the high level instructions in your basic program and the low-level native code your computer’s processor executes. At run time, Visual Basic translates each p-code statement to native code. By compiling directly to native code format, you eliminate the intermediate p-code step.

The compile tab under Project Properties in the Project Menu allows you to select compilation code options.

 

Section Start Page Next Page