BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

OpenProcess() returns different handles

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
Wiebe Vos
Guest





PostPosted: Sun Apr 09, 2006 9:03 pm    Post subject: OpenProcess() returns different handles Reply with quote



BCB6 + WinXP(Pro)

//
PROCESS_INFORMATION processInfo;
//
I sucessfully launch an app with CreateProcess(...)
However thereafter OpenProcess ( PROCESS_ALL_ACCESS, true,
processInfo.dwProcessId)
returns a handle that differs from processInfo.hProcess
(In fact it returns a different handle each time when called.)
Why?

Thanks in advance,
Wiebe
Back to top
Eric Noel
Guest





PostPosted: Sun Apr 09, 2006 10:03 pm    Post subject: Re: OpenProcess() returns different handles Reply with quote



Wiebe Vos wrote:
Quote:
I sucessfully launch an app with CreateProcess(...)
However thereafter OpenProcess ( PROCESS_ALL_ACCESS, true,
processInfo.dwProcessId)
returns a handle that differs from processInfo.hProcess
(In fact it returns a different handle each time when called.)
Why?

WAD. Think of it as if you were opening a file handle - you can open the
same file multiple times, each handle can have different access, and be
used independently. Same idea in this case for your process handle.

- Eric
Back to top
Wiebe Vos
Guest





PostPosted: Mon Apr 10, 2006 9:03 am    Post subject: Re: OpenProcess() returns different handles Reply with quote



Thanks,
I want to use the returned handle to check if the program is (still) alive
and be able to kill it.
However GetExitCodeProcess ( returnedHandle, &exitCode) gives exitCode=0.
How can I get the appropriate handle to do so, when I have the processId?

Wiebe
Back to top
Wiebe Vos
Guest





PostPosted: Mon Apr 10, 2006 9:03 am    Post subject: Re: OpenProcess() returns different handles Reply with quote

BTW. It is a console program. (no window)
Wiebe
Back to top
Wiebe Vos
Guest





PostPosted: Mon Apr 10, 2006 10:03 am    Post subject: Re: OpenProcess() returns different handles Reply with quote

Never mind.
The dwDesiredAccess parameter in OpenProcess() deals with that.
FWIW:
PROCESS_QUERY_INFORMATION gives the handle to determine the program status.
PROCESS_TERMINATE gives the license to kill.

Wiebe
Back to top
Bob Gonder
Guest





PostPosted: Mon Apr 10, 2006 5:03 pm    Post subject: Re: OpenProcess() returns different handles Reply with quote

Wiebe Vos wrote:

Quote:
I want to use the returned handle to check if the program is (still) alive
and be able to kill it.

The hProcess member of the PROCESS_INFORMATION structure you passed to
CreateProcess() should give you that handle.

{Note: I am not recommending this code. It is for illustration
purposes only}

if( CreateProcess( 0, command, 0, 0, FALSE, NORMAL_PRIORITY_CLASS,
0, 0, &si, &pi ) )
{
CloseHandle( pi.hThread );
do{
WaitForSingleObject( pi.hProcess, 100);
GetExitCodeProcess ( pi.hProcess, &exitCode);
}while( exitCode == STILL_ACTIVE );

CloseHandle( pi.hProcess );
};

Quote:
However GetExitCodeProcess ( returnedHandle, &exitCode) gives exitCode=0.

Did you check the return value of the function itself?

if( ! GetExitCodeProcess ( pi.hProcess, &exitCode) )
ReportError( "GetExitCodeProcess", GetLastError() );

One also wonders why you ask for the handle to be inheritable (true)?
> OpenProcess ( PROCESS_ALL_ACCESS, true, processInfo.dwProcessId)
Back to top
Wiebe Vos
Guest





PostPosted: Mon Apr 10, 2006 7:03 pm    Post subject: Re: OpenProcess() returns different handles Reply with quote

Hi Bob,

Quote:
The hProcess member of the PROCESS_INFORMATION structure you passed to
CreateProcess() should give you that handle.

Correct. However thereafter the launched app is bound to be terminated
and/or
"replaced".

Quote:
...why you ask for the handle to be inheritable (true)?
No specific reason. Taken from an example.


As indicated in my earlier response, I managed to find a solution.

Thanks for your help,

Wiebe
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.