Martin v. Löwis | 2d12372 | 2006-04-17 09:46:47 +0000 | [diff] [blame] | 1 | /* This program looks for processes which have build\PCbuild\python.exe |
| 2 | in their path and terminates them. */ |
| 3 | #include <windows.h> |
| 4 | #include <psapi.h> |
| 5 | #include <stdio.h> |
| 6 | |
| 7 | int main() |
| 8 | { |
| 9 | DWORD pids[1024], cbNeeded; |
| 10 | int i, num_processes; |
| 11 | if (!EnumProcesses(pids, sizeof(pids), &cbNeeded)) { |
| 12 | printf("EnumProcesses failed\n"); |
| 13 | return 1; |
| 14 | } |
| 15 | num_processes = cbNeeded/sizeof(pids[0]); |
| 16 | for (i = 0; i < num_processes; i++) { |
| 17 | HANDLE hProcess; |
| 18 | char path[MAX_PATH]; |
| 19 | HMODULE mods[1024]; |
| 20 | int k, num_mods; |
| 21 | hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
| 22 | | PROCESS_VM_READ |
| 23 | | PROCESS_TERMINATE , |
| 24 | FALSE, pids[i]); |
| 25 | if (!hProcess) |
| 26 | /* process not accessible */ |
| 27 | continue; |
| 28 | if (!EnumProcessModules(hProcess, mods, sizeof(mods), &cbNeeded)) { |
| 29 | /* For unknown reasons, this sometimes returns ERROR_PARTIAL_COPY; |
| 30 | this apparently means we are not supposed to read the process. */ |
| 31 | if (GetLastError() == ERROR_PARTIAL_COPY) { |
| 32 | CloseHandle(hProcess); |
| 33 | continue; |
| 34 | } |
| 35 | printf("EnumProcessModules failed: %d\n", GetLastError()); |
| 36 | return 1; |
| 37 | } |
| 38 | if (!GetProcessImageFileName(hProcess, path, sizeof(path))) { |
| 39 | printf("GetProcessImageFileName failed\n"); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | _strlwr(path); |
Martin v. Löwis | ce8607d | 2006-04-17 10:39:39 +0000 | [diff] [blame^] | 44 | /* printf("%s\n", path); */ |
Martin v. Löwis | 2d12372 | 2006-04-17 09:46:47 +0000 | [diff] [blame] | 45 | if (strstr(path, "build\\pcbuild\\python_d.exe") != NULL) { |
| 46 | printf("Terminating %s (pid %d)\n", path, pids[i]); |
| 47 | if (!TerminateProcess(hProcess, 1)) { |
| 48 | printf("Termination failed: %d\n", GetLastError()); |
| 49 | return 1; |
| 50 | } |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | CloseHandle(hProcess); |
| 55 | } |
Martin v. Löwis | ce8607d | 2006-04-17 10:39:39 +0000 | [diff] [blame^] | 56 | } |