From: Jeff McKay on
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?

From: Alexander Grigoriev on
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.

"Jeff McKay" <jeff.mckay(a)comaxis.com> wrote in message
news:kaWdncsQ2bLCcJjWnZ2dnUVZ_qqdnZ2d(a)supernews.com...
>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?
>


From: David Lowndes on
>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?

Keep the process handles that you start and use GetExitCodeProcess and
check for STILL_ACTIVE.

Dave
From: Remy Lebeau on

"Jeff McKay" <jeff.mckay(a)comaxis.com> wrote in message news:kaWdncsQ2bLCcJjWnZ2dnUVZ_qqdnZ2d(a)supernews.com...

> 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.

You don't 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.

> 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.

Not necessarily. If you don't 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.

> What I get instead is that OpenProcess() always works, even after
> I have used Task Manager to kill the child process.

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

--
Remy Lebeau (TeamB)