Results 1 to 4 of 4

Thread: [VB6] Shell & Wait

  1. #1

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Arrow [VB6] Shell & Wait

    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. This function has been fully tested only on XP. Using PeekMessage with a value of -1 for the hWnd parameter seems to be unreliable under Vista and Windows 7, but the documentation makes no mention of this.

    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

    Name:  Wait.png
Views: 16089
Size:  22.0 KB



    UPDATE

    Version 2 of the code now works more reliably under Vista and Windows 7.



    Check out the related code: Function Wait (non-freezing & non-CPU-intensive)
    Attached Files Attached Files
    Last edited by Bonnie West; Sep 13th, 2015 at 03:54 AM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  2. #2
    New Member
    Join Date
    Aug 2015
    Posts
    6

    Re: [VB6] Shell & Wait

    Quote Originally Posted by Bonnie West View Post
    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. This function has been fully tested only on XP. Using PeekMessage with a value of -1 for the hWnd parameter seems to be unreliable under Vista and Win 7, but the documentation makes no mention of this.

    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
    

    Name:  Wait.png
Views: 16089
Size:  22.0 KB


    Check out the related code: Function Wait (non-freezing & non-CPU-intensive)
    I tried this: The ShellWS function wraps the Windows Script Host's Run method as shown below.
    It waited forever, way beyond when the shelled program ended. The only way I could get control back was to close the invoked window. Do you have any suggestions?
    I am using vb5 under win98. My test was executed while using vb5.

  3. #3
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,708

    Re: [VB6] Shell & Wait

    Windows 98? I'm going to go ahead and guess that that's the problem. But I can't find any decent documentation on version differences to say what features are even supported. For all we know the WaitOnReturn could wrap WaitForSingleObject, which wasn't available in Win98.

  4. #4

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [VB6] Shell & Wait

    Quote Originally Posted by hijohnr View Post
    Do you have any suggestions?
    Have you already tried the two other functions? Did they also failed to monitor the shelled program's termination?

    Quote Originally Posted by fafalone View Post
    For all we know the WaitOnReturn could wrap WaitForSingleObject, which wasn't available in Win98.
    I don't have access to a Windows 98 system so I can't confirm this, but in the MSDN Library that came with VS6, the documentation of the WaitForSingleObject function mentions that it "Requires Windows 95 or later."
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width