Last active
November 2, 2021 09:39
-
-
Save dscho/0a68ec9d41d7f4f2796d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <windows.h> | |
#include <tlhelp32.h> | |
static int get_process_tree(DWORD *list, int len, int max_len) | |
{ | |
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | |
PROCESSENTRY32 entry; | |
int i; | |
memset(&entry, 0, sizeof(entry)); | |
entry.dwSize = sizeof(entry); | |
if (!Process32First(snapshot, &entry)) | |
return len; | |
do { | |
for (i = len - 1; i >= 0; i--) | |
if (list[i] == entry.th32ParentProcessID) { | |
list[len++] = entry.th32ProcessID; | |
if (len >= max_len) { | |
fprintf(stderr, "Warning: many pids " | |
"found, stopping...\n"); | |
return len; | |
} | |
} | |
} while (Process32Next(snapshot, &entry)); | |
return len; | |
} | |
int main(int argc, char **argv) | |
{ | |
DWORD list[1024]; | |
int len = 0, i; | |
if (argc < 2) { | |
fprintf(stderr, "Usage: %s <pid>\n", argv[0]); | |
exit(1); | |
} | |
list[len++] = (int) atoi(argv[1]); | |
len = get_process_tree(list, len, sizeof(list) / sizeof(*list)); | |
for (i = len - 1; i >= 0; i--) { | |
HANDLE process = | |
OpenProcess(PROCESS_ALL_ACCESS, FALSE, list[i]); | |
fprintf(stderr, "Killing %d\n", list[i]); | |
if (process) { | |
TerminateProcess(process, 1); | |
CloseHandle(process); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment