GetExitCodeProcess
(1)
HProcessHandle
(1)
CreateProcess
(1)
OpenProcess
(1)
TeamB
(1)
Andcheck
(1)
Reuses
(1)

OpenProcess() question

Asked By Jeff McKay
19-Nov-09 08:13 PM
I have an application that uses CreateProcess to start several child
processes.  I want my main program to
periodically check to see if they are still running.  I tried to do this by
calling OpenProcess() using the process id
of the child process; I assumed I would get a null return from the call if
it was no longer running.   What I get
instead is that OpenProcess() always works, even after I have used Task
Manager to kill the child process.  I have
used this technique in another program, that monitors processes created
elsewhere, but not when I spawn the process
myself.  Any suggestions?

You need to check the process handle state instead.

Alexander Grigoriev replied to Jeff McKay
19-Nov-09 09:50 PM
You need to check the process handle state instead.

WaitForSingleObject(hProcessHandle, 0) will do.

OpenProcess may work for a dead process if there are still open handles to
it.

Keep the process handles that you start and use GetExitCodeProcess andcheck

David Lowndes replied to Jeff McKay
20-Nov-09 04:40 AM
Keep the process handles that you start and use GetExitCodeProcess and
check for STILL_ACTIVE.

Dave

You do not need to use OpenProcess() for that.

Remy Lebeau replied to Jeff McKay
20-Nov-09 02:36 PM
You do not need to use OpenProcess() for that.  CreateProcess() returns =
both the Process ID and Process Handle of the launched process.  Use =
those values as-is.  You can pass the Process Handle to =
GetExitCodeProcess() or any of the WaitFor...() functions to detect if =
the process is running.  Just be sure to close the Process Handle when =
you are done using it.


Not necessarily.  If you do not close the handle that CreateProcess() =
returns, the child process remains alive in memory, even if it is not =
running anymore.  That way, you can call GetExitCodeProcess() and other =
process-related functions on the child process.  Worse, you are =
re-opening the Process ID, which has no clue if the original child =
process is still attached to that ID anymore.  OpenProcess() opens =
whatever current process is using that ID.  The OS can reuses a =
process's ID after the process is fully terminated.  All the more reason =
to use the Process Handle that CreateProcess() gives you instead.


The killed Process ID was likely reused by the OS before you had a =
chance to call OpenProcess().

--=20
Remy Lebeau (TeamB)
Post Question To EggHeadCafe