0
 HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
 if (h != INVALID_HANDLE_VALUE) {
  THREADENTRY32 te;
  te.dwSize = sizeof(te);
  if (Thread32First(h, &te)) {
   do {
     if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
                      sizeof(te.th32OwnerProcessID)) {
                          HANDLE Handle = OpenProcess(
        PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
        FALSE,
        te.th32OwnerProcessID /* This is the PID, you can find one from windows task manager */
    );
                          TCHAR Buffer[MAX_PATH];
       wprintf(L"Process %u IdThred=%u\n",
             te.th32OwnerProcessID, te.th32ThreadID);
     }
   te.dwSize = sizeof(te);
   } while (Thread32Next(h, &te));
  }
  CloseHandle(h);
 }

this code it lists all processes and all threads of the process, but I want it lists only the thread of a process by pid ... example: explorer.exe pid = 5454 through the pid wanted him to have the ids of threads and thread state.

Wellison
  • 19
  • 4
  • I'm not really sure what you're asking, but this [Example](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686701%28v=vs.85%29.aspx) may help. – Retired Ninja Jan 02 '14 at 21:48

1 Answers1

2

Your code actually works. Just needs a little change:

HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (h != INVALID_HANDLE_VALUE)
{
    THREADENTRY32 te;
    te.dwSize = sizeof(te);
    if (Thread32First(h, &te))
    {
        do
        {
            //in THREADENTRY32 structure there is a member called th32OwnerProcessID
            //you can check owner process of thread like this:
            if (te.th32OwnerProcessID == 5454)
            {
                wprintf(L"Process %u IdThred=%u\n", te.th32OwnerProcessID, te.th32ThreadID);
            }
        } while (Thread32Next(h, &te));
    }
    CloseHandle(h);
}
Mustafa Chelik
  • 2,113
  • 2
  • 17
  • 29