Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OpenProcess() question

19 views
Skip to first unread message

Jeff McKay

unread,
Nov 19, 2009, 8:13:55 PM11/19/09
to
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?

Alexander Grigoriev

unread,
Nov 19, 2009, 9:50:25 PM11/19/09
to
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....@comaxis.com> wrote in message
news:kaWdncsQ2bLCcJjW...@supernews.com...

David Lowndes

unread,
Nov 20, 2009, 4:40:21 AM11/20/09
to

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

Dave

Remy Lebeau

unread,
Nov 20, 2009, 2:36:41 PM11/20/09
to

"Jeff McKay" <jeff....@comaxis.com> wrote in message news:kaWdncsQ2bLCcJjW...@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)

0 new messages