如何创建崩溃时重新启动的服务 [英] How to create service which restarts on crash

查看:162
本文介绍了如何创建崩溃时重新启动的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用。将第二个参数设置为 SERVICE_CONFIG_FAILURE_ACTIONS ,并传递 SERVICE_FAILURE_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; //等待5秒后重新启动

ChangeServiceConfig2(hService,SERVICE_CONFIG_FAILURE_ACTIONS,sfa);


I am creating a service using CreateService. The service will run again fine if it happens to crash and I would like to have Windows restart the service if it crashes. I know it is possible to set this up from the services msc see below.

How can I programatically configure the service to always restart if it happens to crash.

解决方案

You want to call ChangeServiceConfig2 after you've installed the service. Set the second parameter to SERVICE_CONFIG_FAILURE_ACTIONS and pass in an instance of SERVICE_FAILURE_ACTIONS as the third parameter, something like this:

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);

这篇关于如何创建崩溃时重新启动的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆