转载:
https://blog.csdn.net/kikaylee/article/details/51395360
/*
@描述:一个简单的Windows守护进程的例子(C++版本)
@作者:kikaylee
@日期:2016-05-13 10:30
*/
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <io.h>
#include <iostream>
using namespace std;
//隐藏DOS黑窗口
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
//定义路径最大程度
#define MAX_PATH 4096
//定义守护进程名称
#define PROCCESS_NAME "test.exe"
//定义写入的注册表路径
#define SELFSTART_REGEDIT_PATH "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\"
//设置本身开机自启动
BOOL SetSelfStart()
{
//获取程序完整名称
char pName[MAX_PATH]={0};
GetModuleFileNameA(NULL,pName,MAX_PATH);
//在注册表中写入启动信息
HKEY hKey=NULL;
LONG lRet=NULL;
lRet=RegOpenKeyExA(HKEY_LOCAL_MACHINE,SELFSTART_REGEDIT_PATH,0,KEY_ALL_ACCESS,&hKey);
//判断是否成功
if(lRet!=ERROR_SUCCESS)
{
return FALSE;
}
lRet=RegSetValueExA(hKey,"testProtect",0,REG_SZ,(const unsigned char*)pName,strlen(pName)+sizeof(char));
//判断是否成功
if(lRet!=ERROR_SUCCESS)
{
return FALSE;
}
//关闭注册表
RegCloseKey(hKey);
return TRUE;
}
int main()
{
//设置程序开机自启动
if(!SetSelfStart())
{
cout<<"守护进程开机自启动失败"<<endl;
return -1;
}
STARTUPINFOA si;
//进程对象
PROCESS_INFORMATION pi;
//初始化
ZeroMemory(&si,sizeof(si));
si.cb=sizeof(si);
ZeroMemory(&pi,sizeof(pi));
//获取当前程序的路径
char pPath[MAX_PATH]={0};
GetCurrentDirectoryA(MAX_PATH,pPath);
//拼接需要守护的程序
strcat(pPath,"\\");
strcat(pPath,PROCCESS_NAME);
//构造cmd执行守护进程的字符串
char pCmd[MAX_PATH]={0};
strcat(pCmd,"cmd /c ");
strcat(pCmd,pPath);
//无限循环,监视守护进程
do{
//检查守护程序是否存在
if(_access(pPath,0)!=-1)
{
//创建子进程,判断是否执行成功
if(!CreateProcessA(NULL,pCmd,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
{
cout<<"守护进程启动失败,程序即将退出"<<endl;
return -1;
}
//启动成功,获取进程的ID
cout<<"守护进程成功,ID:"<<pi.dwProcessId<<endl;
//无限等待子进程退出
WaitForSingleObject(pi.hProcess,INFINITE);
//如果退出了
cout<<"守护进程退出了。。。"<<endl;
//关闭进程和句柄
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
cout<<"守护程序不存在"<<endl;
}
//睡一下,重启
Sleep(2000);
}
while(true);
return 0;
}