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.

Upvotes: 0

Views: 158

Answers (1)

catnip
catnip

Reputation: 25388

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.

Upvotes: 1

Related Questions