Getting a handle to the process's main thread

前端 未结 5 2214
暖寄归人
暖寄归人 2020-12-10 13:06

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 Cre

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 13:20

    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.

    // Take a snapshot of all running threads  
    hThreadSnap = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 ); 
    if( hThreadSnap == INVALID_HANDLE_VALUE ) 
      return( FALSE );
    

    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 the PROCESS_INFORMATION block contains the tid for the primary thread. Welcome any ideas from others.

提交回复
热议问题