Re: How to Enum Threads In a Process? by andrea
andrea
Mon May 10 11:39:15 CDT 2004
here's the copy and paste:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <psapi.h>
#include <time.h>
void PrintProcessNameAndID(DWORD processID) {
HANDLE hProcess;
HMODULE hMod;
DWORD cbNeeded;
char szProcessName[MAX_PATH + 1] = "unknown";
if ( (hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, processID)) != NULL ) {
if ( EnumProcessModules(hProcess, &hMod, sizeof(HMODULE),
&cbNeeded)) {
GetModuleBaseName(hProcess, hMod, szProcessName,
sizeof(szProcessName));
}
printf( "%s (Process ID: %u)\n", szProcessName, processID );
CloseHandle(hProcess);
}
}
void PrintModules( DWORD processID )
{
HMODULE hMods[1024];
HANDLE hProcess;
DWORD cbNeeded;
unsigned int i;
// Print the process identifier.
printf( "\nProcess ID: %u\n", processID );
// Get a list of all the modules in this process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
{
char szModName[MAX_PATH];
// Get the full path to the module's file.
if ( GetModuleFileNameEx( hProcess, hMods[i], szModName,
sizeof(szModName)))
{
// Print the module name and handle value.
printf("\t%s (0x%08X)\n", szModName, hMods[i] );
}
}
}
CloseHandle( hProcess );
}
void PrintMemoryInfo( DWORD processID ) {
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
// Print the process identifier.
printf( "\nProcess ID: %u\n", processID );
// Print information about the memory usage of the process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
{
printf( "\tPageFaultCount: %d\n", pmc.PageFaultCount );
printf( "\tPeakWorkingSetSize: %d\n", pmc.PeakWorkingSetSize );
printf( "\tWorkingSetSize: %d\n", pmc.WorkingSetSize );
printf( "\tQuotaPeakPagedPoolUsage: %d\n",
pmc.QuotaPeakPagedPoolUsage );
printf( "\tQuotaPagedPoolUsage: %d\n", pmc.QuotaPagedPoolUsage );
printf( "\tQuotaPeakNonPagedPoolUsage: %d\n",
pmc.QuotaPeakNonPagedPoolUsage );
printf( "\tQuotaNonPagedPoolUsage: %d\n",
pmc.QuotaNonPagedPoolUsage );
printf( "\tPagefileUsage: %d\n", pmc.PagefileUsage );
printf( "\tPeakPagefileUsage: %d\n", pmc.PeakPagefileUsage );
}
CloseHandle( hProcess );
}
int main(int argc, char* argv[]) {
DWORD aProcesses[1], cbNeeded, cProcesses;
unsigned int i;
if ( EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) {
cProcesses = cbNeeded / sizeof(DWORD);
for ( i = 0; i < cProcesses; i++ ) {
PrintProcessNameAndID( aProcesses[i] );
PrintModules( aProcesses[i] );
PrintMemoryInfo( aProcesses[i] );
}
}
Sleep(1000 * 1000);
return 0;
}
"John" <anonymous@discussions.microsoft.com> wrote in message
news:3200E43B-01A9-46E7-837D-F1EB7379E80B@microsoft.com...
> How can I enum threads in a process and get their exitcode?
>
> Just like in a Vc's debug window.
>
> thanks
>