http://www.rohitab.com/discuss/topic/18490-restart-process-after-user-kills-it/Hey,
I don't know if this is already on the forum somewhere, i'm sure it is, but i didn't have the time to read it all yet
I made a small function that checks if a process is running, and it can be used to restart the process if the users kills it. If you do it right you can make 2 apps that keep checking for each others existence and restart eachother if needed. (or 3 or 4 or ...
).
Here it is, it wont run on windows NT based systems. It did run on my XP. You will need to link against Kernel32.lib (or dev cpp equivalent) by the way.
#include <windows.h>
#include <stdio.h>
#include <tlhelp32.h>
bool procIsActive(char* exeName)
{
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapShot == INVALID_HANDLE_VALUE) return false;
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(PROCESSENTRY32);
BOOL flag = Process32First(hSnapShot, &procEntry);
while (flag)
{
if (!stricmp(procEntry.szExeFile,exeName)) return true;
flag = Process32Next(hSnapShot, &procEntry);
}
return false;
}
int main()
{
while (true){
if (!procIsActive("notepad.exe")) {
system("notepad.exe");
Sleep(1000);
}
Sleep(1);
}
return 0;
}