How to create service which restarts on crash

后端 未结 3 1422
耶瑟儿~
耶瑟儿~ 2021-02-13 21:06

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

3条回答
  •  长情又很酷
    2021-02-13 21:28

    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 buffer(numBytes);
    
    SERVICE_FAILURE_ACTIONS *sfa = reinterpret_cast(&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);
    

提交回复
热议问题