Obtain the master thread ID using the process ID

Source: Internet
Author: User

Obtain the master thread ID using the process ID, which is only applicable to a single thread. Multi-thread should distinguish which is the main thread and distinguish the method to be verified

(1) it seems that the earliest starttime can be used, but the thread execution time is not necessarily reliable. If createthread is started at the beginning, the thread execution time will be the same. The value on the stack can be traced back to determine which thread is the main thread, and the number of stacks of the main thread is somewhat different. The most obvious is the PE entry point on the main thread stack.
Information, This is the sub-thread.
(2) Obtain csrprocessinfo-> clientid. uniquethread from csrprocesslink, which is absolutely reliable.

 

# Include <iostream. h>
# Include <windows. h>
# Include <tlhelp32.h>
Void main ()
{
DWORD dwprocessid, dwthreadid;
While (1)
{
Dwthreadid = 0;
Cout <"Enter the process PID :";
Cin> dwprocessid;
Threadentry32 te32 = {sizeof (te32 )};
Handle hthreadsnap = createconlhelp32snapshot (th32cs_snapthread, 0 );
If (thread32first (hthreadsnap, & te32 ))
{
Do {
If (dwprocessid = te32.th32ownerprocessid)
{
Dwthreadid = te32.th32threadid;
Break;
}
} While (thread32next (hthreadsnap, & te32 ));
}
If (dwthreadid! = 0)
Cout <"main thread ID:" <dwthreadid <Endl;
Else
Cout <"not found" <Endl;
}
}

Assembly Code

Local @ stprocess: processentry32; information of each process
Local @ hsnapshot; snapshot handle

DWORD dwprocessid = XXXXX, dwthreadid = 0;
Threadentry32 te32 = {sizeof (te32 )};
Handle hthreadsnap = createconlhelp32snapshot (th32cs_snapthread, 0 );
If (thread32first (hthreadsnap, & te32 ))
{
Do {
If (dwprocessid = te32.th32ownerprocessid)
{
Dwthreadid = te32.th32threadid;
Break;
}
} While (thread32next (hthreadsnap, & te32 ));
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.