locked
Thread ID from thread handle RRS feed

  • Question

  • I need to call PostThreadMessage on a thread I've only have a handle to it. How can I get the thread id from the thread handle? I can only find about GetThreadId, but its from Vista and I need it to work on XP as well.

    Thank you.-

    K-ballo.-
    Wednesday, September 9, 2009 3:55 AM

Answers

  • There are no happy options here.  You'd have to call the barely documented NtQueryInformationThread().  That requires GetProcAddress() and the undocumented THREAD_BASIC_INFORMATION.ClientId field.

    Hans Passant.
    • Marked as answer by K-ballo Wednesday, September 9, 2009 11:29 PM
    Wednesday, September 9, 2009 11:18 AM

All replies

  • There are no happy options here.  You'd have to call the barely documented NtQueryInformationThread().  That requires GetProcAddress() and the undocumented THREAD_BASIC_INFORMATION.ClientId field.

    Hans Passant.
    • Marked as answer by K-ballo Wednesday, September 9, 2009 11:29 PM
    Wednesday, September 9, 2009 11:18 AM
  • It's a very classic code in Underground and Usenet api boards (see Win32 ng for C code) since NT 3.5 ...
    Wednesday, September 9, 2009 1:22 PM
  • I assume from this you are sending a message to a thread that you haven't created yourself. Since if it was a thread you created you could just use the ID from CreateThread.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    Wednesday, September 9, 2009 1:34 PM
  • I assume from this you are sending a message to a thread that you haven't created yourself. Since if it was a thread you created you could just use the ID from CreateThread.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    I actually create the thread myself by means of Boost.Thread, which does not offer a way to retrieve the thread id, just the handle. My real concern is what this means for Windows users of Boost.Thread and possibly TR1's Thread as well. I could wrap around the function to be invoked to have it call GetCurrentThreadId and store it somewhere, with the overhead and synchronization that this means. I'd rather do this than relay on undocumented functions/structs, but at the end it would be easier to just use plain threads through winapi.
    K-ballo.-
    Wednesday, September 9, 2009 11:29 PM