NOTE: I'm sorry but after posting this code I found that it doesn't work. One problem is that either the function name should be changed to TestStillAlive or all the TestStillAlive references should be changed to TestIfProcessStillAlive, but even after doing that it still doesn't work. Can someone fix it?
============================================================
Here is some code that I picked up someplace.
Code:
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Function TestIfProcessStillAlive(ProcessID As Long) As Boolean
Dim Alive_ones As Long
Dim LoopTroughProcess As Long
Dim AlivePulse As Long
Dim hProcess As Long
Dim lngExitCode As Long
AlivePulse = 0
If ProcessID <> 0 Then
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)
If hProcess <> 0 Then
GetExitCodeProcess hProcess, lngExitCode
Else 'process not found
TestStillAlive = False
Exit Function
End If
'if you open a handle then you have to close it too
CloseHandle (hProcess)
Else
TestStillAlive = False
Exit Function
End If
If lngExitCode = 259 Then
TestStillAlive = True
Else
If lngExitCode = 0 Then TestStillAlive = False
End If
End Sub
To test if shelled process has finished use the code similar to this:
Code:
Dim Handle as long
Handle = shell("MyProgram.exe")
While not TestIfProcessStillAlive(Handle)
<code when process not finished>
wend
[Edited by MartinLiss on 07-31-2000 at 01:24 PM]