🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
7 messages
7 total messages Started by Wiebe Vos Sun, 09 Apr 2006 20:36
OpenProcess() returns different handles
#3968
Author: Wiebe Vos
Date: Sun, 09 Apr 2006 20:36
14 lines
1248 bytes
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 
Re: OpenProcess() returns different handles
#3969
Author: Eric Noel
Date: Sun, 09 Apr 2006 20:54
13 lines
1406 bytes
Wiebe Vos wrote:
> 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
Re: OpenProcess() returns different handles
#3970
Author: Wiebe Vos
Date: Mon, 10 Apr 2006 08:15
7 lines
1164 bytes
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 
Re: OpenProcess() returns different handles
#3971
Author: Wiebe Vos
Date: Mon, 10 Apr 2006 08:22
2 lines
1055 bytes
BTW. It is a console program. (no window)
Wiebe 
Re: OpenProcess() returns different handles
#3972
Author: Wiebe Vos
Date: Mon, 10 Apr 2006 09:20
7 lines
1297 bytes
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 
Re: OpenProcess() returns different handles
#3974
Author: Bob Gonder
Date: Mon, 10 Apr 2006 16:37
32 lines
2077 bytes
Wiebe Vos wrote:

>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 ); 
    };

>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)
Re: OpenProcess() returns different handles
#3977
Author: Wiebe Vos
Date: Mon, 10 Apr 2006 18:40
16 lines
1431 bytes
Hi Bob,

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

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

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads