windows C++守护进程实现


#include "stdafx.h"
#include <iostream>
#include <windows.h>
#pragma comment(linker, "/subsystem:\"windows\"   /entry:\"mainCRTStartup\"") //隐藏控制台窗口
using namespace std;
char* cmd = "./test.exe";


int main()
{
    
    
	STARTUPINFO si;

	PROCESS_INFORMATION pi; 

	ZeroMemory(&si, sizeof(si));
	si.cb = sizeof(si);
	ZeroMemory(&pi, sizeof(pi));
	do {
    
    
		if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
		{
    
    
			//cout << "创建进程失败.." << GetLastError() << endl;
			return 0;
		}

		// 等待进程退出... 
		WaitForSingleObject(pi.hProcess, INFINITE);
		//关闭进程和句柄 
		CloseHandle(pi.hProcess);
		CloseHandle(pi.hThread);
	} while (true);
	exit(0);
	return 0;
}


猜你喜欢

转载自blog.csdn.net/wang03989/article/details/115906548