在windows下创建守护进程A和B ,在其中一个挂掉以后,另一个会把挂掉的拉起来。
下面展示一些 内联代码片
。
这里只列出了A的代码,B和A类似。
#include<iostream>
#include<Windows.h>
#include<tchar.h>
#include <tlhelp32.h>//声明快照函数的头文件
bool IsProcessRun(const WCHAR * pName) //查看进程是否运行
{
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;
bool bFind = false;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
return false;
}
// Set the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);
// Retrieve information about the first process,
// and exit if unsuccessful
if (!Process32First(hProcessSnap, &pe32))
{
CloseHandle(hProcessSnap); // clean the snapshot object
return false;
}
// Now walk the snapshot of processes, and