Results 1 to 3 of 3

Thread: Shell And Wait Question (advanced)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    Shell And Wait Question (advanced)

    I have modified a shell and wait script that checks to see if a NET USE statement (run in a batch file) has taken longer than 7 seconds. However I can't think of a good way to do it. I tried the timer and that doesn't work the way I'm doing it.

    If anyone has a recommendation on a way of doing it let me know.

    Code:
    Private Function shellAndWait(ByVal fileName As String) As Long
        Dim executionStatus As Long
        Dim processHandle As Long
        Timer1.Interval = 1000
        Timer1.Enabled = True
        timercnt = 1
        'Execute the application/file, but HIDE the window
        executionStatus = Shell(fileName, vbNormalFocus)
        
        'Get the Process Handle
        processHandle = OpenProcess(&H100000, True, executionStatus)
     
        'Wait till the application is finished
        Do
            returnValue = WaitForSingleObject(processHandle, 50)
            DoEvents
            If bCancel Then
                SendMessage FindWindow(WinCmdPath, vbNullString), WM_CLOSE, 0&, 0&
                returnValue = -1
                Exit Do
            End If
        Loop Until returnValue = 0
        If returnValue = 0 Then
            Timer1.Enabled = False
            timercnt = 1
        End If
        bCancel = False
        shellAndWait = returnValue
    End Function
    Private Sub Timer1_Timer()
        If timercnt = 7 Then
            bCancel = True
            timercnt = 1
        Else
            timercnt = timercnt + 1
        End If
    End Sub

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Shell And Wait Question (advanced)

    Why don't you use this instead of a timer. it's much more simple:

    Code:
       t = Timer
       Do
          ret = WaitForSingleObject(processHandle, 50)
          DoEvents
       Loop Until (ret = 0) Or (Timer - t > 7)
    
       If ret <> 0 Then
          SendMessage FindWindow(WinCmdPath, vbNullString), WM_CLOSE, 0&, 0&
          ShellAndWait = -1
       Else
          ShellAndWait = 0
       End If

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    29

    Re: Shell And Wait Question (advanced)

    I actually ended up with this:

    Code:
    Private Function shellAndWait(sShellFile As String) As Long
        sFile = sShellFile
        lProcessID = Shell(sFile, vbHide)
        
        If lProcessID <> 0 Then
            lHandle = OpenProcess(SYNCHRONIZE, 0, lProcessID)
            If lHandle <> 0 Then
                lReturn = WaitForSingleObject(lHandle, 5000)
                If lReturn > 0 Then
                    ErrorCode = "30"
                    ErrorStatus = 1
                    Call ErrorHandling(rMachine, lUsername, i, ErrorCode)
                    ' Get the target's window handle.
                    target_hwnd = FindWindow(vbNullString, WinCmdPath)
                    If target_hwnd = 0 Then
                        Exit Function
                    End If
                    ' Send the application the WM_CLOSE message.
                    PostMessage target_hwnd, WM_CLOSE, 0, 0
                    CloseHandle (lHandle)
                End If
            End If
        End If
        shellAndWait = returnValue
    End Function

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