All,
Background:
I have developed an application that retrieves downloaded files and send them to another program via the Shell function. The app calls Shell, retrieves a process id, and then stores that process id in a database. Later the app goes out and checks to see if the process id is still out there. If not, I know that the process has ended. This method allows me to send many files at once.
Problem:
I have noticed on occasion that the Shell function will return a process id that does not correspond to the program I am expecting. For instance, I may find that one of the process ids I received corresponds to 'Explorer.exe'. As a results my app thinks the program I Shelled is still running. Has any one ever seen this? Is there a way to prevent it? Is it possible that someone is running some other program at the exact same time I Shell, therefore causing me to get the wrong process id?
Code:
'//This is the code I use with Shell, sProgram represents the program I am calling.
Dim lTaskId As Long
lTaskId = Shell(sProgram)
'//This is the code I use to check if the process is still active.
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Function IsProcessOpen(ByVal lTaskId As Long, Optional ByVal lMsecs As Long = 0) As Boolean
'Test to see if a process is still running by examining the process handle after attempting to open the process. If the value is 0, the process has either failed or completed.
Dim lProcHandle As Long
'Get the process handle.
lProcHandle = OpenProcess(&H100000, True, lTaskId)
'Close the handle.
CloseHandle lProcHandle
If lProcHandle <> 0 Then IsProcessOpen = True
End Function
Thanks for your input.
--Lee
Background:
I have developed an application that retrieves downloaded files and send them to another program via the Shell function. The app calls Shell, retrieves a process id, and then stores that process id in a database. Later the app goes out and checks to see if the process id is still out there. If not, I know that the process has ended. This method allows me to send many files at once.
Problem:
I have noticed on occasion that the Shell function will return a process id that does not correspond to the program I am expecting. For instance, I may find that one of the process ids I received corresponds to 'Explorer.exe'. As a results my app thinks the program I Shelled is still running. Has any one ever seen this? Is there a way to prevent it? Is it possible that someone is running some other program at the exact same time I Shell, therefore causing me to get the wrong process id?
Code:
'//This is the code I use with Shell, sProgram represents the program I am calling.
Dim lTaskId As Long
lTaskId = Shell(sProgram)
'//This is the code I use to check if the process is still active.
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Function IsProcessOpen(ByVal lTaskId As Long, Optional ByVal lMsecs As Long = 0) As Boolean
'Test to see if a process is still running by examining the process handle after attempting to open the process. If the value is 0, the process has either failed or completed.
Dim lProcHandle As Long
'Get the process handle.
lProcHandle = OpenProcess(&H100000, True, lTaskId)
'Close the handle.
CloseHandle lProcHandle
If lProcHandle <> 0 Then IsProcessOpen = True
End Function
Thanks for your input.
--Lee