1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#include <windows.h> #include <tlhelp32.h> #include <tchar.h> #include <stdio.h>
BOOL GetProcessList(); BOOL ListProcessModules(DWORD dwPID); BOOL ListProcessThreads(DWORD dwOwnerPID); void printError(TCHAR const* msg);
int main(void) { GetProcessList(); return 0; } BOOL GetProcessModule(DWORD dwPID, char* szExeName, LPMODULEENTRY32 lpMe32, DWORD cbMe32) { BOOL bRet = FALSE; BOOL bFound = FALSE; HANDLE hModuleSnap = NULL; MODULEENTRY32 me32 = { 0 };
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID); if (hModuleSnap == INVALID_HANDLE_VALUE) return (FALSE);
me32.dwSize = sizeof(MODULEENTRY32);
if (Module32First(hModuleSnap, &me32)) { do { if (strcmp(szExeName, me32.szModule) == 0) { CopyMemory(lpMe32, &me32, cbMe32); bFound = TRUE; } } while (!bFound && Module32Next(hModuleSnap, &me32));
bRet = bFound; } else bRet = FALSE;
CloseHandle(hModuleSnap);
return (bRet); }
BOOL GetProcessList() { HANDLE hProcessSnap = NULL; BOOL bRet = FALSE; PROCESSENTRY32 pe32 = { 0 }; hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == INVALID_HANDLE_VALUE) { return (FALSE); } pe32.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hProcessSnap, &pe32)) { DWORD dwPriorityClass; BOOL bGotModule = FALSE; MODULEENTRY32 me32 = { 0 }; do { bGotModule = GetProcessModule(pe32.th32ProcessID, pe32.szExeFile, &me32, sizeof(MODULEENTRY32)); if (bGotModule) { HANDLE hProcess; hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID); dwPriorityClass = GetPriorityClass(hProcess); CloseHandle(hProcess); printf("PID\t\t\t%d\n", pe32.th32ProcessID); printf("Thread Count\t\t%d\n", pe32.cntThreads); printf("Module Name\t\t%s\n", me32.szModule); printf("Full Path\t\t%s\n\n", me32.szExePath);
} } while (Process32Next(hProcessSnap, &pe32)); bRet = TRUE; } else bRet = FALSE; CloseHandle(hProcessSnap); return (bRet); }
BOOL ListProcessModules(DWORD dwPID) { HANDLE hModuleSnap = INVALID_HANDLE_VALUE; MODULEENTRY32 me32;
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID); if (hModuleSnap == INVALID_HANDLE_VALUE) { printError(TEXT("CreateToolhelp32Snapshot (of modules)")); return(FALSE); }
me32.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(hModuleSnap, &me32)) { printError(TEXT("Module32First")); CloseHandle(hModuleSnap); return(FALSE); }
do { _tprintf(TEXT("\n\n MODULE NAME: %s"), me32.szModule); _tprintf(TEXT("\n Executable = %s"), me32.szExePath); _tprintf(TEXT("\n Process ID = 0x%08X"), me32.th32ProcessID); _tprintf(TEXT("\n Ref count (g) = 0x%04X"), me32.GlblcntUsage); _tprintf(TEXT("\n Ref count (p) = 0x%04X"), me32.ProccntUsage); _tprintf(TEXT("\n Base address = 0x%08X"), (DWORD)me32.modBaseAddr); _tprintf(TEXT("\n Base size = %d"), me32.modBaseSize);
} while (Module32Next(hModuleSnap, &me32));
CloseHandle(hModuleSnap); return(TRUE); }
BOOL ListProcessThreads(DWORD dwOwnerPID) { HANDLE hThreadSnap = INVALID_HANDLE_VALUE; THREADENTRY32 te32;
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); if (hThreadSnap == INVALID_HANDLE_VALUE) return(FALSE);
te32.dwSize = sizeof(THREADENTRY32);
if (!Thread32First(hThreadSnap, &te32)) { printError(TEXT("Thread32First")); CloseHandle(hThreadSnap); return(FALSE); }
do { if (te32.th32OwnerProcessID == dwOwnerPID) { _tprintf(TEXT("\n\n THREAD ID = 0x%08X"), te32.th32ThreadID); _tprintf(TEXT("\n Base priority = %d"), te32.tpBasePri); _tprintf(TEXT("\n Delta priority = %d"), te32.tpDeltaPri); _tprintf(TEXT("\n")); } } while (Thread32Next(hThreadSnap, &te32));
CloseHandle(hThreadSnap); return(TRUE); }
void printError(TCHAR const* msg) { 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), sysMsg, 256, NULL);
p = sysMsg; while ((*p > 31) || (*p == 9)) ++p; do { *p-- = 0; } while ((p >= sysMsg) && ((*p == '.') || (*p < 33)));
_tprintf(TEXT("\n WARNING: %s failed with error %d (%s)"), msg, eNum, sysMsg); }
|