get thread id from a thread handle

get thread id from a thread handle

Post by Qm9iIExpZ2 » Sat, 05 Jun 2004 20:26:02


I have a thread handle passed to a function and I need to get its id. The handle might not be for the current thread so I can't use GetCurrentThreadId()

Any suggestions are good (even if it means checking every thread in the process).
 
 
 

get thread id from a thread handle

Post by Tom Stewar » Sat, 05 Jun 2004 21:46:31

DWORD dwTID = GetThreadId ( hThread ); // check for zero



so I can't use GetCurrentThreadId().

 
 
 

get thread id from a thread handle

Post by Tom Stewar » Sat, 05 Jun 2004 21:55:19

Oops, I see this is W2K3 only. Probably doesn't help...
 
 
 

get thread id from a thread handle

Post by Gary Chans » Sun, 06 Jun 2004 03:09:03


handle might not be for the current thread so I can't use
GetCurrentThreadId().
process).

Use the Tool Help Library and walk through the threads of the process
using Thread32First/Thread32Next.

--
-GJC [MS Windows SDK MVP]
-Software Consultant (Embedded systems and Real Time Controls)
- http://www.yqcomputer.com/
XXXX@XXXXX.COM
 
 
 

get thread id from a thread handle

Post by Tom Stewar » Sun, 06 Jun 2004 04:51:46

What do you need the thread id for? Who's passing you the handle -- any chance "they" can also pass the ID?
 
 
 

get thread id from a thread handle

Post by Tom Stewar » Sun, 06 Jun 2004 04:56:23

Still, what's he going to do to match that up with his handle? Is there some function he can call with the handle that
will return something definitive he can cross-reference to the output of Thread32First/Next?
 
 
 

get thread id from a thread handle

Post by Gary Chans » Sun, 06 Jun 2004 06:13:04


some function he can call with the handle that
Thread32First/Next?

I thought I had a good answer to that but I don't. It would probably
require using undocumented APIs.

--
-GJC [MS Windows SDK MVP]
-Software Consultant (Embedded systems and Real Time Controls)
- http://www.yqcomputer.com/
XXXX@XXXXX.COM
 
 
 

get thread id from a thread handle

Post by George M. » Sun, 06 Jun 2004 07:40:09

Bob,

You can use NtQueryInformationThread on 2000 and XP. Use GetThreadId()
where supported.

Regards,

George.
 
 
 

get thread id from a thread handle

Post by Qm9iIExpZ2 » Wed, 09 Jun 2004 01:01:05

I have a system so that I can thread specific variables that can be accessed by any thread. I've implemented it for arrays so that the first thread has an internal value 0, the second thread is 1 etc, so that i can have, say my_var[_Internal::ThreadId(GetCurrentThreadId())]. I then tried to hook the Win32 thread functions so that they allocate and free memory automatically, e.g

void _ExitThread(DWORD ErrLevel)
_Internal::DelThread(GetCurrentThreadId())
ExitThread(ErrLevel)
return


and then
#define ExitThread(ErrLevel) _ExitThread(ErrLevel

The problem is when I tried TerminateThread, because you pass a handle to the thread

It doesn't matter if can't save about 1k of memory on calls to _TerminateThread because I only call it when things go wrong anyway.
 
 
 

get thread id from a thread handle

Post by Tom Stewar » Wed, 09 Jun 2004 01:42:48

If it's all within your own process, you could create another column in your thread table to hold the handle for the
thread. Then, you could do a linear search through the table for that particular handle. I'm assuming that you maintain
a single handle per thread, perhaps even the one returned by createthread...

Given that this is an exceptional case, the penalty of the linear search isn't that bad.



arrays so that the first thread has an internal value 0, the second thread is 1 etc, so that i can have, say
my_var[_Internal::ThreadId(GetCurrentThreadId())]. I then tried to hook the Win32 thread functions so that they allocate
and free memory automatically, e.g:
wrong anyway.
 
 
 

get thread id from a thread handle

Post by Carl Danie » Wed, 09 Jun 2004 02:11:13


http://www.yqcomputer.com/ %20Functions/NT%20Objects/Thread/NtQueryInformationThread.html

-cd
 
 
 

get thread id from a thread handle

Post by Gary Chans » Wed, 09 Jun 2004 03:56:27


accessed by any thread. I've implemented it for arrays so that the first
thread has an internal value 0, the second thread is 1 etc, so that i can
have, say my_var[_Internal::ThreadId(GetCurrentThreadId())]. I then tried to
hook the Win32 thread functions so that they allocate and free memory
automatically, e.g:
the thread.
_TerminateThread because I only call it when things go wrong anyway.

If that's what you're trying to do, why not just use Thread Local
Storage?

--
-GJC [MS Windows SDK MVP]
-Software Consultant (Embedded systems and Real Time Controls)
- http://www.yqcomputer.com/
XXXX@XXXXX.COM