0

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

1

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.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • casting away `const` seems like a bad idea. [Some Windows APIs actually do overwrite their input parameters](https://stackoverflow.com/q/11339186/103167) so passing string literals to arguments not declared `LPC(T|W)STR` is not advisable. – Ben Voigt Jul 29 '20 at 16:17
  • @BenVoigt Quite right, what _was_ I thinking? Edited, thank you. – Paul Sanders Mar 02 '22 at 12:15