Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

execute outside application and wait until finished 1

Status
Not open for further replies.

Pistol34

MIS
Nov 17, 1999
55
US
I was wondering what the quickest way would be to shell out to a different application then wait for it to finish and continue my code. I have done this using the PID and a loop, but there must be a simpilar way than the Win32 API.

Thanks,

Pete
 
Set oWiSH = CreateObject("WScript.Shell")
iReturn = oWiSH.Run("Notepad.exe",1,True)
Set oWiSH = Nothing

The Run method arguments are: String name of executable you want to shell to (and generally you need to include the path - Notepad is in the Windows default path so it's not necessary); Window style (minimized, active, etc.); and a boolean argument 'bWaitOnReturn' - when it's set to true your code will pause until the shell has executed. iReturn is the error code that will return after shell execution - if the command runs successfully this will be 0.

You can find more at this site under WindowsScriptHost:
I believe the VB Shell command works about the same way and uses the same arguments.
 
API Version
Code:
'==== bas module - declarations section ========
Option Explicit

Private Declare Function OpenProcess Lib "kernel32" _
       (ByVal dwDesiredAccess As Long, _
        ByVal bInheritHandle As Long, _
        ByVal dwProcessId As Long) As Long
Private Declare Function WaitForInputIdle Lib "user32" _
       (ByVal hProcess As Long, +
        ByVal dwMilliseconds As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" _
        (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
         (ByVal hObject As Long) As Long

Private Const INFINITE = &HFFFF
Private Const SYNCHRONIZE = &H100000

'==== bas modue - code section ====
Public Sub ShellAndWait(sAppPath As String, Optional _
                        iWindowStyle As VbAppWinStyle = vbMinimizedFocus, _
    Optional  lmsTimeOut As Long = INFINITE)
    'Optional argument lmsTimeOut specifies milliseconds to wait.
   'Wait forever if omitted (default).
    Dim lPid As Long, hProc As Long, lTimeOut As Long

    lPid = Shell(sAppPath, iWindowStyle)
    hProc = OpenProcess(SYNCHRONIZE, 0&, lPid)
    If hProc <> 0& Then
        WaitForInputIdle hProc, INFINITE
        WaitForSingleObject hProc, lmsTimeOut
        CloseHandle hProc
    End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top