/*************************************************************** Stop a running process, read it from "stop.ini" (c) Peter van Eerten, December 24, 2006 Licensed under the GPL license. Use at own risk. This 'stop.exe' looks for a 'stop.ini' containing the PID to terminate. Also all child process are killed. 3/1/2007: now the INI file will be deleted automatically ***************************************************************/ #include #include #include #define MAX_LENGTH 1024 /* * Kill a Process identified by the pid * */ void Kill_PID(DWORD pid) { HANDLE hProc; hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid); if (hProc == NULL){ MessageBox(NULL, "ERROR: Could not open (child-)process! Exiting...", "Error!", MB_OK | MB_ICONERROR); exit(EXIT_FAILURE); } if (!TerminateProcess(hProc, 0)){ MessageBox(NULL, "ERROR: Could not stop your application! Exiting...", "Error!", MB_OK | MB_ICONERROR); exit(EXIT_FAILURE); } CloseHandle(hProc); } /* * Main program * */ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { /* Declarations for current filename */ char filename[MAX_PATH]; DWORD len; char *end; /* Declarations for processes */ FILE *pidini; char txt[MAX_LENGTH]; BOOL cont; DWORD pid; HANDLE hSnapShot; PROCESSENTRY32 procentry; /* Determine name of INI file */ len=GetModuleFileName(NULL, filename, MAX_PATH); end=filename+len-1; /* Check format */ if (strstr(filename, ".") == NULL){ MessageBox(NULL, "ERROR: Wrong name of executable! Exiting...", "Error!", MB_OK | MB_ICONERROR); exit(EXIT_FAILURE); } /* Put the INI extension behind the filename */ while (*end!='.') end--; end++; *end='i'; end++; *end='n'; end++; *end='i'; end++; *end='\0'; /* Open the INI file */ pidini = fopen(filename, "r"); /* Does the INI file exist? */ if (pidini != NULL){ /* Yes, get the string from the INI file */ fgets(txt, MAX_LENGTH, pidini); fclose(pidini); /* Get the PID and kill it */ pid = (DWORD)atoi(txt); Kill_PID(pid); /* Find all childprocesses and terminate them without mercy */ hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); procentry.dwSize = sizeof(PROCESSENTRY32); cont = Process32First(hSnapShot, &procentry ) ; while (cont) { if (pid == procentry.th32ParentProcessID) { pid = procentry.th32ProcessID; Kill_PID(pid); cont = Process32First(hSnapShot, &procentry); } else { procentry.dwSize = sizeof(PROCESSENTRY32); cont = Process32Next(hSnapShot, &procentry); } } /* Remove the INI file again */ if(remove("stop.ini" ) == -1 ) MessageBox(NULL, "WARNING: Could not delete the INI file!", "Warning!", MB_OK | MB_ICONWARNING); } /* The INI file does not exist? */ else { MessageBox(NULL, "ERROR: Cannot find the INI file!", "Error!", MB_OK | MB_ICONERROR); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }