简体   繁体   English

当C++中的Windows中的服务崩溃时如何运行程序?

[英]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.我知道我应该使用微软提供的SERVICE_FAILURE_ACTIONSChangeServiceConfig2() But, I got stuck using them because I can not find any example of using these structure and function.但是,我无法使用它们,因为我找不到任何使用这些结构和 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 .其中,如文档所述, service_handle是通过调用OpenService获得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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