The Process Tool Help Library 5

 

 

 

Traversing the Thread List Program Example

 

The following example function lists running threads for a specified process. First, the ListProcessThreads() function takes a snapshot of the currently executing threads in the system using CreateToolhelp32Snapshot(), and then it walks through the list recorded in the snapshot using the Thread32First() and Thread32Next() functions. The parameter for ListProcessThreads() is the process identifier of the process whose threads are to be listed.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

 

Traversing the Thread List Program Example: Creating new C++ empty Win32 console mode application using VC++

 

 

Then, add the source file and give it a suitable name.

 

Traversing the Thread List Program Example: Adding new C++ source file

 

Next, add the following source code.

 

#include <windows.h>

#include <tlhelp32.h>

#include <stdio.h>

 

//  Forward declarations:

BOOL ListProcessThreads( DWORD dwOwnerPID);

void printError(WCHAR* msg);

 

int wmain()

{

  ListProcessThreads(GetCurrentProcessId());

}

 

BOOL ListProcessThreads(DWORD dwOwnerPID)

{

  HANDLE hThreadSnap = INVALID_HANDLE_VALUE;

  THREADENTRY32 te32;

 

  // Take a snapshot of all running threads 

  hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);

  if(hThreadSnap == INVALID_HANDLE_VALUE)

    return(FALSE);

 

  // Fill in the size of the structure before using it.

  te32.dwSize = sizeof(THREADENTRY32);

 

  // Retrieve information about the first thread,

  // and exit if unsuccessful

  if(!Thread32First(hThreadSnap, &te32))

  {

    printError(LThread32First\n); // Show cause of failure

    CloseHandle(hThreadSnap);     // Must clean up the snapshot object!

    return(FALSE);

  }

 

  // Now walk the thread list of the system,

  // and display information about each thread

  // associated with the specified process

  do

  {

    if(te32.th32OwnerProcessID == dwOwnerPID)

    {

      wprintf(L\n\n     THREAD ID      = 0x%08X\n, te32.th32ThreadID);

      wprintf(L\n     base priority  = %d\n, te32.tpBasePri);

      wprintf(L\n     delta priority = %d\n, te32.tpDeltaPri);

    }

  } while(Thread32Next(hThreadSnap, &te32));

  //  Don't forget to clean up the snapshot object.

  CloseHandle(hThreadSnap);

  return(TRUE);

}

 

void printError(WCHAR* msg)

{

  DWORD eNum;

  WCHAR sysMsg[256];

  WCHAR* p;

 

  eNum = GetLastError();

  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,

         NULL, eNum,

         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language

         sysMsg, 256, NULL);

 

  // Trim the end of the line and terminate it with a null

  p = sysMsg;

  while((*p > 31) || (*p == 9))

    ++p;

  do

 {

        *p-- = 0;

  } while((p >= sysMsg) && ((*p == '.') || (*p < 33)));

 

  // Display the message

  wprintf(L\n  WARNING: %s failed with error %d (%s)\n, msg, eNum, sysMsg);

}

 

Build and run the project. The following screenshot is a sample output.

 

Traversing the Thread List Program Example: A sample console output

 

 

 

< Windows Process Tool Help APIs 4 | Windows Process Tool Help APIs | Win32 Programming | Windows Process Tool Help APIs 6 >