Check to see if batch file is still running

Hi,

I am calling ShellExecute API of shell32.dll in order to execute a batch file with parameters. However, i am trying to halt my program until the batch file finishes running. I have tried several approaches to get the handle of the window in order to loop until the batch file terminates, but it seems like the cmd.exe window is not a normal window and i am unable to do so.

Does  anyone know how to check to see if the window is still open?

Thanks!
da4thrzaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rettiseertConnect With a Mentor Commented:
do you need to show the window of the batch file?

then use
shell "c:\bat.bat", vbMaximizedFocus
0
 
rettiseertCommented:
Try this:


Option Explicit

Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long


Private Sub Form_Load()

    Dim PID As Long

    PID = Shell("c:\x.bat 1")
   
    Do While IsProcessRuning(PID)
        DoEvents
    Loop
   
    MsgBox "Bat ended"
   
End Sub



Public Function IsProcessRuning(PID As Long) As Boolean

   
    Dim hp As Long
    Dim lngExitCode As Long
   
    Const PROCESS_QUERY_INFORMATION As Long = &H400
    Const STILL_ACTIVE = &H103
   
    hp = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, PID)
   
    If GetExitCodeProcess(hp, lngExitCode) Then
        If lngExitCode = STILL_ACTIVE Then
            IsProcessRuning = True
        End If
    End If


End Function
0
 
da4thrzaAuthor Commented:
Awesome... that worked. Thanks a lot.

The only problem is that the batch file runs in the background. Is there a way to make it the active window in the foreground?
0
 
da4thrzaAuthor Commented:
you, my friend, rock! Thanks a lot
0
All Courses

From novice to tech pro — start learning today.