Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1459

[VB6] Shell & Wait

$
0
0
The module included in the attachment contains 3 routines that supplements VB's intrinsic Shell function by waiting for the shelled program to terminate before resuming with the next line of code without blocking other events from firing. The accompanying Form demonstrates usage of each function.

The Shell_n_Wait function augments the native Shell function by waiting for the shelled process until it exits. It also auto-expands environment variables before relaying them to Shell. Instead of retrieving the Process ID/Task ID, Shell_n_Wait returns the terminated process' Exit Code.

The ShellW function is probably the most flexible yet easy-to-use shelling routine ever coded. It accepts Unicode paths to executables or documents (registered file types). Paths can be fully qualified or relative and may contain navigational elements ("." or ".."), environment variables and/or arguments/parameters. The window style (normal, minimized, maximized, hidden, etc.) can be optionally requested. It features the ability to wait for the shelled program indefinitely or exactly as specified. Its return value depends on whether the shelled process is still alive or not. If the process still exists, it retrieves the Process ID, otherwise it returns the Exit Code.

The ShellWS function wraps the Windows Script Host's Run method as shown below:

Code:


'Runs a program in a new process.
Public Function ShellWS(ByRef Command As String, Optional ByVal WindowStyle As VbAppWinStyle = vbNormalFocus, _
                                                Optional ByVal WaitOnReturn As Boolean) As Long
    #Const Referenced = True
    #If Not Referenced Then
        ShellWS = CreateObject("WScript.Shell").Run(Command, WindowStyle, WaitOnReturn)
    #Else
        With New
WshShell
            ShellWS = .Run(Command, WindowStyle, WaitOnReturn)
        End With
    #End If      'Adapted from "Best Shell & Wait (No API's!)" by Matthew Roberts
End Function    'http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=8349&lngWId=1

Attached Images
 
Attached Files

Viewing all articles
Browse latest Browse all 1459

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>