Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何创建在崩溃时重新启动的服务_C++_Windows_Winapi_Service - Fatal编程技术网

C++ 如何创建在崩溃时重新启动的服务

C++ 如何创建在崩溃时重新启动的服务,c++,windows,winapi,service,C++,Windows,Winapi,Service,我正在使用创建一个服务。该服务将再次正常运行,如果它发生崩溃,我想让Windows重新启动服务,如果它崩溃。我知道可以从下面的服务msc设置此设置 如何以编程方式配置服务,使其在发生崩溃时始终重新启动。您希望在安装服务后调用。将第二个参数设置为SERVICE\u CONFIG\u FAILURE\u ACTIONS,并将的实例作为第三个参数传递,如下所示: int numBytes = sizeof(SERVICE_FAILURE_ACTIONS) + sizeof(SC_ACTION); s

我正在使用创建一个服务。该服务将再次正常运行,如果它发生崩溃,我想让Windows重新启动服务,如果它崩溃。我知道可以从下面的服务msc设置此设置

如何以编程方式配置服务,使其在发生崩溃时始终重新启动。

您希望在安装服务后调用。将第二个参数设置为
SERVICE\u CONFIG\u FAILURE\u ACTIONS
,并将的实例作为第三个参数传递,如下所示:

int numBytes = sizeof(SERVICE_FAILURE_ACTIONS) + sizeof(SC_ACTION);
std::vector<char> buffer(numBytes);

SERVICE_FAILURE_ACTIONS *sfa = reinterpret_cast<SERVICE_FAILURE_ACTIONS *>(&buffer[0]);
sfa.dwResetPeriod = INFINITE;
sfa.cActions = 1;
sfa.lpsaActions[0].Type = SC_ACTION_RESTART;
sfa.lpsaActions[0].Delay = 5000; // wait 5 seconds before restarting

ChangeServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, sfa);
int numBytes=sizeof(服务失败动作)+sizeof(服务失败动作);
std::向量缓冲区(单位:字节);
服务失败动作*sfa=重新解释强制转换(&buffer[0]);
sfa.dwResetPeriod=无限期;
sfa.cActions=1;
sfa.lpsaActions[0]。Type=SC\u ACTION\u RESTART;
sfa.LPSA操作[0]。延迟=5000;//等待5秒钟后再重新启动
ChangeServiceConfig2(hs服务、服务配置、故障操作、sfa);

以上答案将为您提供要点。。。但它不会编译

尝试:


使用了Deltanine的方法,但对其进行了一些修改,以便能够控制每个故障动作:

SERVICE_FAILURE_ACTIONS servFailActions;
SC_ACTION failActions[3];

failActions[0].Type = SC_ACTION_RESTART; //Failure action: Restart Service
failActions[0].Delay = 120000; //number of seconds to wait before performing failure action, in milliseconds = 2minutes
failActions[1].Type = SC_ACTION_RESTART;
failActions[1].Delay = 120000;
failActions[2].Type = SC_ACTION_NONE;
failActions[2].Delay = 120000;

servFailActions.dwResetPeriod = 86400; // Reset Failures Counter, in Seconds = 1day
servFailActions.lpCommand = NULL; //Command to perform due to service failure, not used
servFailActions.lpRebootMsg = NULL; //Message during rebooting computer due to service failure, not used
servFailActions.cActions = 3; // Number of failure action to manage
servFailActions.lpsaActions = failActions;

ChangeServiceConfig2(sc_service, SERVICE_CONFIG_FAILURE_ACTIONS, &servFailActions); //Apply above settings
SERVICE_FAILURE_ACTIONS servFailActions;
SC_ACTION failActions[3];

failActions[0].Type = SC_ACTION_RESTART; //Failure action: Restart Service
failActions[0].Delay = 120000; //number of seconds to wait before performing failure action, in milliseconds = 2minutes
failActions[1].Type = SC_ACTION_RESTART;
failActions[1].Delay = 120000;
failActions[2].Type = SC_ACTION_NONE;
failActions[2].Delay = 120000;

servFailActions.dwResetPeriod = 86400; // Reset Failures Counter, in Seconds = 1day
servFailActions.lpCommand = NULL; //Command to perform due to service failure, not used
servFailActions.lpRebootMsg = NULL; //Message during rebooting computer due to service failure, not used
servFailActions.cActions = 3; // Number of failure action to manage
servFailActions.lpsaActions = failActions;

ChangeServiceConfig2(sc_service, SERVICE_CONFIG_FAILURE_ACTIONS, &servFailActions); //Apply above settings