For more detailed information, see the Visual Basic topic Shell Function.
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.
Failure to Start
If the Shell function cannot start the named program, a 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.
Waiting for Completion
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.
Determining the Exit Code
A process can return an exit code when it terminates. However, you cannot use Shell to retrieve this exit code, because Shell returns zero if it waits for termination, and also because the process runs in a different object from Shell.
To retrieve the exit code from a process, you must write your own code to initiate the process and wait for termination. The following example shows how to initiate a process, wait for it to terminate, and retrieve its exit code.
Dim procID As Integer
Dim newProc As Diagnostics.Process
newProc = Diagnostics.Process.Start("C:\WINDOWS\NOTEPAD.EXE")
procID = newProc.Id
newProc.WaitForExit()
Dim procEC As Integer = -1
If newProc.HasExited Then
procEC = newProc.ExitCode
End If
MsgBox("Process with ID " & CStr(ProcID) & _
" terminated with exit code " & CStr(procEC))
Protecting the File Specification
You should always enclose the entire path and file specification in quotation marks, as the following example shows.
ID = Shell("""C:\Program Files\display.exe"" -a -q", , True, 100000)
Each pair of adjacent double quotation marks (" ") within the string literal is interpreted as one double quotation character in the string. Therefore, the preceding example presents the following string to the Shell function:
"C:\Program Files\display.exe" -a -q
If you did not have the path enclosed in quotation marks, Windows would look for a file called Program.exe in the C:\ directory, instead of display.exe in the C:\Program Files directory.
Security Note: |
|---|
If you do not enclose the path and file specification in quotation marks, there is a security risk if the file name or a path node contains spaces. In the preceding example, the path node
\Program Files includes a space. If the specification were not inside quotation marks and a program named Program.exe had been installed in C:\, for example by illicit tampering, Windows would execute it instead of display.exe.
|
Security Note: |
|---|
The
Shell function requires unmanaged code permission, which might affect its execution in partial-trust situations. For more information, see SecurityPermission and Code Access Permissions.
|