#include <windows.h> #include <tlhelp32.h> #include <stdio.h> void printError(const char *format, ...) { DWORD eNum; TCHAR sysMsg[256]; TCHAR* 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 ) ) ); char msg[1024]; va_list arg; va_start(arg, format); int charSize = sprintf(msg, format, arg); va_end(arg); // Display the message printf( "/n WARNING: %s failed with error %d (%s)/n", msg, eNum, sysMsg ); } BOOL ListProcessThread(DWORD dwOwnerPID) { HANDLE hThreadSnap = NULL; BOOL bRet = FALSE; THREADENTRY32 te32 = {0}; // Take a snapshot of all threads currently in the system. hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); if (hThreadSnap == (HANDLE)-1) { printError("CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD)"); return (FALSE); } // Fill in the size of the structure before using it. te32.dwSize = sizeof(THREADENTRY32); // Walk the thread snapshot to find all threads of the process. // If the thread belongs to the process, add its information // to the display list. if (Thread32First(hThreadSnap, &te32)) { do { if (te32.th32OwnerProcessID == dwOwnerPID) { printf( "/n TID %d", te32.th32ThreadID); printf( "/n Owner PID %d", te32.th32OwnerProcessID); printf( "/n Delta Priority %d", te32.tpDeltaPri); printf( "/n Base Priority %d/n", te32.tpBasePri); bRet = TRUE; } }while (Thread32Next(hThreadSnap, &te32)); } else { printError("Thread32First(hThreadSnap)"); bRet = FALSE; // could not walk the list of threads } // Do not forget to clean up the snapshot object. CloseHandle (hThreadSnap); return (bRet); } BOOL ListProcessModules( DWORD dwPID ) { BOOL bRet = FALSE; HANDLE hModuleSnap = INVALID_HANDLE_VALUE; MODULEENTRY32 me32; // Take a snapshot of all modules in the specified process. hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID ); if( hModuleSnap != INVALID_HANDLE_VALUE ) { // Set the size of the structure before using it. me32.dwSize = sizeof( MODULEENTRY32 ); // Retrieve information about the first module, // and exit if unsuccessful if( Module32First( hModuleSnap, &me32 ) ) { // Now walk the module list of the process, // and display information about each module do { printf( "/n/n MODULE NAME: %s", me32.szModule ); printf( "/n executable = %s", me32.szExePath ); printf( "/n process ID = 0x%08X", me32.th32ProcessID ); printf( "/n ref count (g) = 0x%04X", me32.GlblcntUsage ); printf( "/n ref count (p) = 0x%04X", me32.ProccntUsage ); printf( "/n base address = 0x%08X", (DWORD) me32.modBaseAddr ); printf( "/n base size = %d", me32.modBaseSize ); } while( Module32Next( hModuleSnap, &me32 ) ); bRet = TRUE; } else { printError( "Module32First" ); // Show cause of failure } // Do not forget to clean up the snapshot object. CloseHandle( hModuleSnap ); } else { printError( "CreateToolhelp32Snapshot (of modules %d)", dwPID); } return( bRet ); } BOOL GetProcessList () { HANDLE hProcessSnap = NULL; BOOL bRet = FALSE; PROCESSENTRY32 pe32 = {0}; // Take a snapshot of all processes in the system. hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == (HANDLE)-1) { printError( "CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS)"); return (FALSE); } // Fill in the size of the structure before using it. pe32.dwSize = sizeof(PROCESSENTRY32); // Walk the snapshot of the processes, and for each process, // display information. if (Process32First(hProcessSnap, &pe32)) { DWORD dwPriorityClass; do { // Get the actual priority class. HANDLE hProcess; hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID); dwPriorityClass = GetPriorityClass (hProcess); CloseHandle (hProcess); // Print the process's information. printf( "/nPriority Class Base/t%d/n", pe32.pcPriClassBase); printf( "PID/t/t/t%d/n", pe32.th32ProcessID); printf( "Thread Count/t/t%d/n", pe32.cntThreads); ListProcessThread(pe32.th32ProcessID); ListProcessModules(pe32.th32ProcessID); }while (Process32Next(hProcessSnap, &pe32)); bRet = TRUE; } else { printError( "Process32First(hProcessSnap)" ); bRet = FALSE; // could not walk the list of processes } // Do not forget to clean up the snapshot object. CloseHandle (hProcessSnap); return (bRet); }