简体   繁体   中英

How to run a program when a service crashes in Windows in C++?

I would like to run another program when a service crashes. I know that I should use SERVICE_FAILURE_ACTIONS and ChangeServiceConfig2() provided by Microsoft. But, I got stuck using them because I can not find any example of using these structure and function.

I would appreciate it if anyone can show me an example to implement this.

It's not so hard. You need something like this:

SC_ACTION sc_actions = { SC_ACTION_RUN_COMMAND, 0 };
TCHAR command [] = __T ("path/to/command");
SERVICE_FAILURE_ACTIONS failure_actions =
    { INFINITE, NULL, command, 1, &sc_actions);
BOOL ok = ChangeServiceConfig2 (service_handle,
    SERVICE_CONFIG_FAILURE_ACTIONS, &failure_actions);

Where, as the documentation states, service_handle is obtained by a call to OpenService .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM