当打开一个进程A的时候,此进程检测是否存在进程B,如果不存在就创建进程B。
进程B的作用是检测进程A是否被关闭,如果被关闭了,就再创建一个进程A。
双进程守护A.exe代码如下:
#include "pch.h" #include <iostream> #include <windows.h> int main() { while (TRUE) { HWND hWnd = FindWindow(NULL, L"双进程守护B"); if (hWnd==NULL) { STARTUPINFO StartupInfo = { sizeof(STARTUPINFO) }; PROCESS_INFORMATION ProcessInformation = { 0 }; CreateProcess(L"..\Debug\双进程守护B.exe", NULL, NULL, NULL, NULL, NULL, NULL, NULL, &StartupInfo, &ProcessInformation); WaitForSingleObject(ProcessInformation.hProcess, -1); } else { DWORD dwPid = 0; GetWindowThreadProcessId(hWnd, &dwPid); HANDLE hProc = OpenProcess( SYNCHRONIZE, NULL, dwPid); WaitForSingleObject(hProc, -1); } } }
双进程守护B.exe代码如下:
#include "pch.h" #include <iostream> #include <windows.h> int main() { while (TRUE) { HWND hWnd = FindWindow(NULL, L"双进程守护A"); if (hWnd == NULL) { STARTUPINFO StartupInfo = { sizeof(STARTUPINFO) }; PROCESS_INFORMATION ProcessInformation = { 0 }; CreateProcess(L"..\Debug\双进程守护A.exe", NULL,NULL, NULL, NULL, NULL,NULL, NULL, &StartupInfo, &ProcessInformation); WaitForSingleObject( ProcessInformation.hProcess, -1); } else { DWORD dwPid = 0; GetWindowThreadProcessId(hWnd, &dwPid); HANDLE hProc = OpenProcess(SYNCHRONIZE, NULL, dwPid); WaitForSingleObject(hProc, -1); } } }