VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Starts a Program and waits for it to Terminate, Also works with DOS by monitoring the window for fi

by Ilya Pokrovskiy (1 Submission)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 4th February 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Starts a Program and waits for it to Terminate, Also works with DOS by monitoring the window for finished. Works on all MS systems. All done

API Declarations


Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type

Private Const WAIT_OBJECT_0 = 0
Dim TerminateProc As Long

Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As Long, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDriectory As String, _
ByRef lpStartupInfo As STARTUPINFO, _
ByRef lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Private Declare Function EnumThreadWindows Lib "user32" (ByVal dwThreadId As Long, _
ByVal lpfn As Long, _
ByVal lParam As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Rate Starts a Program and waits for it to Terminate, Also works with DOS by monitoring the window for fi



Dim ret As Long, ret1 As Long

Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION

  TerminateProc = 0
    ret = CreateProcess(0, cmd, 0, 0, 0, 0, 0, path, si, pi)
        If ret Then
                  ret = WaitForSingleObject(pi.hProcess, 300)
              Do
                  DoEvents
                  DoEvents
                  DoEvents
                      ret = WaitForSingleObject(pi.hProcess, 300)
                      ret1 = EnumThreadWindows(pi.dwThreadId, AddressOf CloseWindows, 33)
                          If TerminateProc = 1 Then
                                ret1 = TerminateProcess(pi.hProcess, 1)
                                If ret1 <> 0 Then ret = 0
                          End If
              Loop While ret <> WAIT_OBJECT_0
              ret = CloseHandle(pi.hThread)
              ret = CloseHandle(pi.hProcess)
        End If
End Sub
Public Function CloseWindows(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim sWindowTitle As String * 257, ApiRet As Long
    ApiRet = GetWindowText(hwnd, sWindowTitle, 256)
        If LCase(Mid(sWindowTitle, 1, 8)) = "finished" Then
              TerminateProc = 1
              CloseWindows = 0
              Exit Function
        End If
CloseWindows = 1
End Function






Download this snippet    Add to My Saved Code

Starts a Program and waits for it to Terminate, Also works with DOS by monitoring the window for fi Comments

No comments have been posted about Starts a Program and waits for it to Terminate, Also works with DOS by monitoring the window for fi. Why not be the first to post a comment about Starts a Program and waits for it to Terminate, Also works with DOS by monitoring the window for fi.

Post your comment

Subject:
Message:
0/1000 characters