Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

How to Determine if two handles refer to the same object

Back to Forum: Tech Off
  • User profile image

    Here is the scenario:

    HANDLE hHandle1 = CreateThread(...);

    now, at some point in the program, you want to know if the current thread is the same as the thread pointed to by hHandle1.

    So, you do

    HANDLE hHandle2;
    DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &hHandle2, 0, FALSE, DUPLICATE_SAME_ACCESS);

    The problem is that you end up having two different handles that might refer to the same thread.

    Is there a way to determine if they both do refer to the same thread?

    I tried getting the thread Ids from both handles and compare them. But they are different, event when I know the two handles refer to the same thread.

    Is there a user mode function similar to the kernel mode function ObReferenceObjectByHandle(); This KM function returns the same pointer for both handles.

  • User profile image

    Nevermind. I use the thread IDs to uniquely identify each thread. It still wound be nice to know how to determine if two handles refer to the same object.

  • User profile image

    Depends on how much control you have over the object itself.  I was faced with this recently in a system where I could not use .net (and therefor couldn't get the hash code) because the handle referred to an object behind the p/Invoke boundary.  We settled on a function that would take the two handles, set a member variable in the object pointed to by one of the handles and then read it from the other.  If they matched, the objects were the same. 

    The value we set was a newly created guid, so there was little chance of duplication.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.