在论坛上看到看到一个要做两个进程可以相互守护的问题,我在这里写了一对,没事大家研究一下.
狼狈为奸的一对程序,两个程序可以相互守护,无法被关掉.
我测试了这两个程序,他们可以相互守侯,所以用任务管理器是关不掉的,要想关掉要用特殊的进程控制程序,半年前我写了一个,很好用,其实就是把其中一个的所有线程都挂起,这样WaitForSingleObject不会返回,然后先关掉另一个,再关掉这个挂起的就可以了
注意,下面是两个程序哦.
另外improper这个词就是"下流,狼狈为奸的含义"
//lbwj_m.cpp
//主程序,将要被守护的程序,这个程序也同时守护"守护程序"
#include "windows.h"
#include "PROCESS.H"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
typedef struct tag_keep_det
{
long lRun;
char carrExePath[300];
}
KEEP_DET;
unsigned long __stdcall KeepImproper_m(void *pvar)
{
KEEP_DET *pKeep;
char carrCmd[1024];
char carrThisExeName[512];
PROCESS_INFORMATION objPro;
long lmid;
SECURITY_ATTRIBUTES objPSecurityAttr, objTSecurityAttr;
STARTUPINFO objStart;
if(0 == pvar)
{
return 0;
}
pKeep = (KEEP_DET*)pvar;
//设置一些属性,我也不了解这是什么,但这样可以保证正确
objPSecurityAttr.bInheritHandle = 1;
objPSecurityAttr.lpSecurityDescriptor = 0;
objPSecurityAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
objTSecurityAttr.bInheritHandle = 1;
objTSecurityAttr.lpSecurityDescriptor = 0;
objTSecurityAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
memset((void*)&objStart, 0, sizeof(STARTUPINFO));
objStart.cb = sizeof(STARTUPINFO);
//pKeep->lRun是一个标志而已
while(pKeep->lRun != 0)
{
memset((void*)carrThisExeName, 0, 512);
memset((void*)carrCmd, 0, 1024);
objPro.hProcess = 0;
GetModuleFileName(0, carrThisExeName, 520);//得到本程序的路径.
carrCmd[0] = 0;
sprintf(carrCmd, "-%s /%ld", carrThisExeName, getpid());//getpid()得到本程序的ID
//把程序的路径,和ID写成命令行传递给守护进程,
//'-'开始的是要守护程序的路径,'/'开始是要守护进程的ID
//启动守护进程
lmid = CreateProcess(pKeep->carrExePath, carrCmd, /
&objPSecurityAttr, &objTSecurityAttr, 0, /
NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE, /
0, 0, &objStart, &objPro);//objStart这个参数一定要有
//objPro.hProcess是守护进程的句柄
if((0 == lmid) || (0 == objPro.hProcess))
{
return 0;
}
else
{
//当守护进程OVER的时候WaitForSingleObject会返回
//然后WHILE循环重新启动守护进程
WaitForSingleObject(objPro.hProcess, INFINITE);
CloseHandle(objPro.hProcess);
}
}
return 0;
}
KEEP_DET g_obj_Keep = {1, {"F:/testcode/lbwj/Debug/improper.exe/0/0"}};//字符串是守护进程的路径
main()
{
long ThreadID;
printf("/nhello! this main");
//启动一个线程,用来保护自己的守护进程
CreateThread(0, 0, KeepImproper_m, (void*)&g_obj_Keep, 0, (DWORD*)&ThreadID);
//想做什么做在,我在这里死循环.
while(1)
{
Sleep(5000);
}
return 0;
}
//end
//守护进程,他守护主进程
//improper.cpp
#include "windows.h"
#include "WINBASE.H"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
main(int iarg, char *parg[])
{
long i;
char carrKeepName[512];
int iKeepId;
HANDLE hProcess;
PROCESS_INFORMATION objPro;
SECURITY_ATTRIBUTES objPSecurityAttr, objTSecurityAttr;
STARTUPINFO objStart;
//循环解吸命令行,'-'开始的是要守护程序的路径,'/'开始是要守护进程的ID
for(i = 0; i < iarg; ++i)
{
printf("/n%ld:%s", i, parg[i]);
if('/' == parg[i][0])
{
//命令行里的ID是字符串,所以要拼成INT型
//之所以传ID而不是进程句柄,
//是因为有些系统跨进程的话HANDLE句柄回不可用(即使设置继承属性),
//但ID全局可用
iKeepId = atoi(parg[i]+1);
}
if('-' == parg[i][0])
{
sprintf(carrKeepName, "%s", parg[i]+1);
}
}
if(0 != iKeepId)
{
//得到要守护进程的句柄
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 1, iKeepId);
if(0 != hProcess)
{
//当要守护进程退出的时候WaitForSingleObject会返回
WaitForSingleObject(hProcess, INFINITE);
CloseHandle(hProcess);
}
objPSecurityAttr.bInheritHandle = 1;
objPSecurityAttr.lpSecurityDescriptor = 0;
objPSecurityAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
objTSecurityAttr.bInheritHandle = 1;
objTSecurityAttr.lpSecurityDescriptor = 0;
objTSecurityAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
memset((void*)&objStart, 0, sizeof(STARTUPINFO));
objStart.cb = sizeof(STARTUPINFO);
//启动要守护的进程
CreateProcess(carrKeepName, 0, /
&objPSecurityAttr, &objTSecurityAttr, 0, /
NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE, /
0, 0, &objStart, &objPro);
//注意这里不用循环,直接退出,
//要守护的进程被CreateProcess启动后会重新启动一个守护进程
}
return 0;
}
//end