views:

77

answers:

3

i have to variable of HANDLE type . first variable is a process HANDLE (with name hProcess) that have'nt PROCESS_QUERY_INFORMATION access right. second variable is a process HANDLE (with name hwndProcess) too that i opened via OpenProcess function and have PROCESS_QUERY_INFORMATION access right. i sure both process is a one. but when i compare as below return false; if (hProcess==hwndProcess) {do something} how should i do?

+1  A: 

There is not an explicit way to check whether two handles refer to the same process. The only way would be to query the process information and check that, e.g. using GetProcessId on each handle to check the process IDs.

If you don't have the necessary access rights to call the desired query functions then you can try calling DuplicateHandle to get a new handle with more access rights. However, if this fails then you have no way of telling whether the handles are to the same process or not.

Anthony Williams
syntax of DuplicateHandle is below.BOOL WINAPI DuplicateHandle( __in HANDLE hSourceProcessHandle, __in HANDLE hSourceHandle, __in HANDLE hTargetProcessHandle, __out LPHANDLE lpTargetHandle, __in DWORD dwDesiredAccess, __in BOOL bInheritHandle, __in DWORD dwOptions);dwOptions can get one of two following values (DUPLICATE_CLOSE_SOURCE or DUPLICATE_SAME_ACCESS) or any combination of those.now can i join those values with PROCESS_QUERY_INFORMATION tag.
Phoenix
Set dwOptions as 0, and set dwDesiredAccess to the required access rights (e.g. PROCESS_QUERY_INFORMATION and whatever else you need).
Anthony Williams
A: 

hProcess must not hold the ProcessHandle of the Process that will be closed. It can and will most times be NULL. I'm doing something similar to get the PIDs of terminated processes.
if((hProcess == NULL) || (hProcess == GetCurrentProcess())){
pid = GetCurrentProcessId();
} else {
pid = ProcessHandleToId(hProcess); }

Are your sure, that it's an access rights problem and your function doesn't fail, because the handle is NULL?

christian
A: 

Is it possible to use GetProcessID with the two handles and then check if both handles cause GetProcessID to return the same process id?

Chubsdad