I have created an additional thread in some small testing app and want to suspend the main thread from this additional thread. The additional thread is created via CreateRemoteThread
from an external process.
Since SuspendThread
needs a HANDLE
to the thread which should be suspended, I want to know how to get this HANDLE
from code running in my additional thread.
A number of useful API functions of this type are under the (of course!) Tool Help suite. The
CreateToolhelp32Snapshot()
API will take a snapshot of the running threads for a specified process.Full example code here.
The struct returned does not differentiate the primary thread from the others. I do not know a mechanism to do so; while some versions of the C runtime will all ExitProcess() at the end of the primary thread, in all recent versions the process continues to run until the last thread exits.
Interjay's recommendation to use GetThreadTimes may be the best bet. If you can
CreateProcess()
the target process, the hThread member of thePROCESS_INFORMATION
block contains the tid for the primary thread. Welcome any ideas from others.Get the thread id with this function:
Simple open the thread to get the handle:
Why don't you just create a program-wide global (use extern if you have to)
On the first line of main, (before any threads are created) do
You can use any form of IPC to share either the id or the HANDLE with the remote process (haven't verified sharing the HANDLE will work but it should!)
I don't think there is anything that differentiates the main thread from other threads once the process has started. However, you can enumerate all threads in the process, and use GetThreadTimes to find the thread with the earliest creation time. Call OpenThread to get a
HANDLE
from a thread ID.