Problem with OpenProcess() in Server 2003/XP

  • Thread starter Thread starter Schmidtmayer Marc
  • Start date Start date
S

Schmidtmayer Marc

Hi all,

We are migrating from NT to Server 2003/XP.
Now I have a problem with an existing service/application.
This application starts some EXE's/CMD's with CreateProcess() and
'remembers' their PID's.
Then each thread checks if its PID is still 'active' by calling the
OpenProcess()-function and with the PID as argument.
That way, we can decide if the process is still active.

So, under NT ... no problem !
BUT under XP ... PROBLEM !!!
Under XP, the OpenProcess() KEEPS RETURNING A HANDLE EVEN WHEN THE
PROCESS IS NOT ACTIVE ANYMORE !!!!

These posts mention the same problem, but there was never a reaction :
http://groups.google.be/[email protected]&rnum=1
http://groups.google.be/[email protected]&rnum=37

Can anyone help me out please ?
Can it be fixed or is there a 'work-around' ?
Help is very welcome !

Thanks,
Marc.
 
Schmidtmayer said:
Hi all,

We are migrating from NT to Server 2003/XP.
Now I have a problem with an existing service/application.
This application starts some EXE's/CMD's with CreateProcess() and
'remembers' their PID's.
Then each thread checks if its PID is still 'active' by calling the
OpenProcess()-function and with the PID as argument.
That way, we can decide if the process is still active.

That is very strange and insecure way to do that. You may e.g. get into
race conditions where the process terminates and a new process is
created with the same pid and your OpenProcess() call will return a
correct handle even if the process you wanted to check is already
terminated.
So, under NT ... no problem !
BUT under XP ... PROBLEM !!!
Under XP, the OpenProcess() KEEPS RETURNING A HANDLE EVEN WHEN THE
PROCESS IS NOT ACTIVE ANYMORE !!!!

Could be some kind of race condition like the one I mentioned, but that
not sure, it could be something keeping the process open.
These posts mention the same problem, but there was never a reaction :
http://groups.google.be/[email protected]&rnum=1
http://groups.google.be/[email protected]&rnum=37

Can anyone help me out please ?

It is far better to save the _handle_ to the process than saving the
pid. The handle will always point to the right process, even after it is
terminated because the process object is available until the last handle
to it is closed.

Then, to check if the process has terminated, call
WaitForSingleObject(hProcess, 0) and see wether WAIT_TIMEOUT or
WAIT_OBEJCT_0 is returned, or call GetExitCodeProcess() and see if the
exit code is STILL_ACTIVE (not preferred way since an application
actually may exit with the code STILL_ACTIVE).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top