Apperantly, GetThreadId is a Vista API. How can I get a thread's id on pre vista systems?
If you can somehow make the thread in question call GetCurrentThreadId and store it somewhere, you could read the result.
There are a few options:
- When you call CreateThread, you get the handle back.
- You can call GetCurrentThreadId to get the current thread's ID.
- You can use Thread32First/Thread32Next to enumerate threads.
If the thread in question enters an alertable wait state frequently, you could send it an APC with QueueUserAPC; the APC handler can then call GetCurrentThreadId and communicate the result back to the caller using whatever method you like.
You can also do this with undocumented NT functions. Using NtQueryInformationThread() on the ThreadBasicInformation class will give you the thread ID in the returned structure. An example can be found in the wine source. However, I'm not sure what versions of windows this is available on - keep in mind these undocumented functions can change at any time, so it's best to test them on the older versions of windows you're interested in, and simply use GetThreadId() where it's available.
Note that these undocumented functions can only be accessed by LoadLibrary() and GetProcAddress() on NTDLL; they have no import library. According to MSDN, declarations for the data structures can be found in Winternl.h
, but if not, just define them yourselves based on the ntinternals links above.