Runs an executable program and returns an integer containing the program's process ID if it is still running.
Public Function Shell( _ ByVal Pathname As String, _ Optional ByVal Style As AppWinStyle = AppWinStyle.MinimizedFocus, _ Optional ByVal Wait As Boolean = False, _ Optional ByVal Timeout As Integer = -1 _ ) As Integer
The Style argument can have one of the following settings:
Enumeration value | Description |
---|---|
AppWinStyle.Hide | The window is hidden and focus is given to the hidden window. |
AppWinStyle.NormalFocus | The window is given focus and displayed in its most recent size and position. |
AppWinStyle.MinimizedFocus | The window is given focus and displayed as an icon. |
AppWinStyle.MaximizedFocus | The window is given focus and displayed using the entire screen. |
AppWinStyle.NormalNoFocus | The window is set to its most recent size and position. The currently active window remains in focus. |
AppWinStyle.MinimizedNoFocus | The window is displayed as an icon. The currently active window remains in focus. |
Exception type | Error number | Condition |
---|---|---|
5 | Style is not within range 0 through 9, inclusive. | |
53 | Shell cannot start the named program. |
The return value of the Shell function depends on whether the program named in Pathname is still executing when Shell returns. If you set Wait to True and the program finishes before the timeout expires, Shell returns zero. If the timeout expires, or if you omit Wait or set it to False, Shell returns the process ID of the program. The process ID is a unique number that identifies the running program.
If the Shell function cannot start the named program, a System.IO.FileNotFoundException error occurs. This can happen, for example, when you attempt to run a 16-bit program, such as command.com
, from an application using System.Windows.Forms. For a workaround, you can run a 32-bit program that calls the desired 16-bit program. In the case of command.com
, you can run cmd.exe
as an alternative.
By default, the Shell function runs the program asynchronously. This means that a program started with the Shell function might not finish executing before the statements following the Shell function are executed. If you want to wait for the program to finish before you continue, set Wait to True.
If the Pathname argument supplies the directory path as well as the file name, it is a good idea to enclose the entire specification in quotes, as the following example shows:
ID = Shell("""C:\Program Files\MyFile.exe"" -a -q", , True, 100000)
Each pair of adjacent double quotes ("") within the string literal is interpreted as one double quote character in the string. Therefore, the preceding example presents the following string to the Shell function:
"C:\Program Files\MyFile.exe" -a -q
If you did not have the path enclosed in quotes, Windows would look for a file called Program.exe
in the C:\ directory. If such a program had been installed at that location, for example by illicit tampering, it would be executed instead of MyFile.exe
in the C:\Program Files directory.
This example uses the Shell function to run an application specified by the user. Specifying AppWinStyle.NormalFocus as the second argument opens the application in normal size and gives it the focus.
Dim ProcID As Integer ' Run Calculator. ProcID =Shell(
"C:\WINDOWS\CALC.EXE",
AppWinStyle.NormalFocus)
AppActivate Function |