How to compare two variables of type HANDLE

I have a variable of type HANDLE. The first HANDLE variable is the HANDLE of the process (named hProcess), which does not have PROCESS_QUERY_INFORMATION permission. The second variable is also the HANDLE of the process (named hwndProcess), which I opened through the OpenProcess function and have the PROCESS_QUERY_INFORMATION access right. I am sure that both processes should have the same handle. But when I compare them, as shown below, it returns a lie; if (hProcess == hwndProcess) {do something} How do I do this?

+3
source share
3 answers

There is no explicit way to check if two descriptors belong to the same process. The only way is to request process information and check this, for example. using GetProcessIdon each descriptor to check process identifiers.

If you do not have the necessary rights to call the required request functions, you can try calling DuplicateHandleto get a new descriptor with a large number of access rights. However, if this fails, you cannot tell if the descriptors of the same process are being processed.

+5
source

hProcess ProcessHandle , . NULL. - , PID .
  if((hProcess == NULL) || (hProcess == GetCurrentProcess())){
pid = GetCurrentProcessId();
} else {
pid = ProcessHandleToId(hProcess); }

, , NULL?

0

The Windows 10 SDK has CompareObjectHandles (HANDLE, HANDLE), which returns TRUE if the descriptors refer to the same underlying kernel object. And you do not need to worry about access rights.

0
source

Source: https://habr.com/ru/post/1760030/


All Articles