How do I convert a thread ID into a thread handle?

A call to GetWindowThreadProcessId has given me the thread identifier that I wanted, but in order to call GetThreadContext I need a thread handle.

I'm experimenting using Get/SetThreadContext and I'm trying to use these API's without actually debugging the process.

Anyone have any thoughts on this,

Tom
Posted on 2001-09-22 17:43:10 by Tom -- Unregistered
I think you have to use OpenProcess:


HANDLE OpenProcess(
DWORD dwDesiredAccess, // access flag
BOOL bInheritHandle, // handle inheritance flag
DWORD dwProcessId // process identifier
);

Where dwDesiredAccess in your case should be PROCESS_SET_INFORMATION (but I'm not sure ); ) .
Posted on 2001-09-23 15:49:55 by LuHa
Translated from MSDN,

invoke OpenThread, THREAD_SET_CONTEXT | THREAD_GET_CONTEXT, FALSE, threadid

should allow you to use both GetThreadContext and SetThreadContext.
Posted on 2001-09-24 14:28:10 by tank
Sounds weird, tank. I found no OpenThread api in MSDN, but I did
find the following. (Q127992).


There is no way to get the thread handle from the thread ID. While there is an OpenProcess() API that takes a PID and returns the handle to the process, there is no corresponding OpenThread() that takes a thread ID and returns a thread handle.


Q121093 mentions an OpenThread, but...

Win32s supports a new function, OpenThread(), which is not a part of the Win32 API.


You could try loading it with GetProcAddress and see what happens. At least it's exported from my kernel32.dll on win2k...
Posted on 2001-09-24 16:05:29 by f0dder
After having read your post, tank I have looked on MSDN web site, since in my MSDN cd (it is rather old) I didn't find any info on OpenThread, and I have found this link:
http://support.microsoft.com/support/kb/articles/Q127/9/92.asp
Is OpenThread available on any Win32 opereting systems?
Posted on 2001-09-24 16:12:17 by LuHa
LuHa, that's the Q127992 I was talking about. Q121093 can be
found here:
http://support.microsoft.com/directory/article.asp?ID=KB;EN-US;Q121093

Reading that, I get the impression a GetModuleHandle followed by
GetProcAddress might be enough. It's probably then a "push threadid"
and "call eax", and you might be lucky :).
Posted on 2001-09-24 16:24:10 by f0dder
Thankyou F0dder, I posted my comment before I read yours :).
Posted on 2001-09-24 16:49:21 by LuHa
Oops!
Didn't see the requirements:

Win2k or ME.

The information is here.
Posted on 2001-09-24 17:04:19 by tank
Hm, win32s was that crappy semi-32bit support on win3.x, wasn't it?
Because Q121093 says it's supported on win32s. This would mean
it's supported on win3.x, Me and 2k? *giggle*.
Posted on 2001-09-24 17:15:40 by f0dder