Restart Process after user kills it

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;
}


posted on 2011-03-08 23:08 挑灯看剑 阅读(270) 评论(0)  编辑 收藏 引用 所属分类: C/C++

只有注册用户登录后才能发表评论。
<2011年3月>
272812345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

公告

【自我介绍】 08年南开大学硕士毕业 最近关注:算法、Linux、c++、高并发 爱好:滑旱冰、打乒乓球、台球、保龄球

常用链接

随笔分类(139)

文章分类

我常去的网站

技术博客(都是大牛)

技术站点

搜索

积分与排名