This week is Computer Science Education Week. As part of their Hour of Code, I will be posting my source code from as far as back as possible.
It’s a bunch of gunk and fudges, so don’t judge me. BTW… everything is BSD licensed.
After years of having to do it either manually through Task Manager or using a scripting engine like AutoIT, I finally settled down and wrote my own Win32 application that does massacres processes that I don’t like. Will post source code later. Note that this kills all instances of the specified process. Usage : killproc.exe […]
killproc.c
#include<windows.h>
#include<process.h>
#include <Tlhelp32.h>
#include<winbase.h>
#include<stdio.h>
#include<string.h>
void killProcessByName(const char*filename){
HANDLE hSnapShot =CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
PROCESSENTRY32 pEntry;
pEntry.dwSize =sizeof(pEntry);
BOOL hRes =Process32First(hSnapShot,&pEntry);
while(hRes){if(strcmp(pEntry.szExeFile, filename)==0){
HANDLE hProcess =OpenProcess(PROCESS_TERMINATE,0,(DWORD) pEntry.th32ProcessID);
if(hProcess != NULL){
TerminateProcess(hProcess,9);
CloseHandle(hProcess);
}
}
hRes =Process32Next(hSnapShot,&pEntry);
}
CloseHandle(hSnapShot);
}
int main(int argc, char * argv[]){
if (argc>1)
{
killProcessByName(argv[1]);
printf (“Killing process named %s\n”,argv[1]);
} else
{
printf (“Usage : killproc <procname>\n”);
}
return 0;
}