开发者

How to Compare Two variable of HANDLE type

开发者 https://www.devze.com 2023-01-12 01:37 出处:网络
I have a variable of HANDLE type. First HANDLE variable is a process HANDLE (with name hProcess) that does not have PROCESS_QUERY_INFORMATION access right.

I have a variable of HANDLE type. First HANDLE variable is a process HANDLE (with name hProcess) that does not have PROCESS_QUERY_INFORMATION access right. Second variable is a process HANDLE (with name hwndProcess) too that I have opened via OpenProcess function and have PROCESS_Q开发者_C百科UERY_INFORMATION access right. I am sure both processes should have same handle. But when i compare them as below, it returns false; if (hProcess==hwndProcess) {do something} How shall I do it?


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.


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?


The Windows 10 SDK has CompareObjectHandles(HANDLE, HANDLE) which returns TRUE if the handles refer to the same underlying kernel object. And you don't have to worry about access rights.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号