获取进程主线程的句柄
我在一些小型测试应用程序中创建了一个附加线程,并希望从该附加线程中挂起主线程。附加线程是通过外部进程的 CreateRemoteThread 创建的。
由于 SuspendThread
需要一个指向应挂起的线程的 HANDLE
,我想知道如何从在我的附加线程中运行的代码获取此 HANDLE
。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为一旦进程启动,主线程与其他线程就没有任何区别。但是,您可以枚举进程中的所有线程,并使用GetThreadTimes 查找最早的线程创建时间。调用 OpenThread 获取
来自线程 ID 的 HANDLE
。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.使用此函数获取线程 ID:
简单地打开线程以获取句柄:
Get the thread id with this function:
Simple open the thread to get the handle:
为什么不创建一个程序范围的全局变量(使用 extern 如果必须的话)
在 main 的第一行,(在创建任何线程之前)
您可以使用 任何形式的 IPC 与远程进程共享 id 或 HANDLE(尚未验证共享 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!)
许多此类有用的 API 函数位于(当然!)工具帮助套件。
CreateToolhelp32Snapshot()
< /a> API 将为指定进程的正在运行的线程拍摄快照。完整示例代码此处。
返回的结构不区分主线程和其他线程。我不知道有什么机制可以做到这一点;虽然某些版本的 C 运行时将所有 ExitProcess() 都放在主线程末尾,但在所有最新版本中,该进程将继续运行,直到最后一个线程退出。
Interjay 建议使用 GetThreadTimes 可能是最好的选择。如果您可以
CreateProcess()
目标进程,PROCESS_INFORMATION
块包含主线程的 tid。欢迎其他人提出任何想法。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.