从 C++ 注册关闭前通知的正确方法 [英] Correct way to register for pre-shutdown notification from C++

查看:56
本文介绍了从 C++ 注册关闭前通知的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 C++ 编写了一个本地服务应用程序,但我找不到注册关闭前通知的正确方法(适用于 Windows XP 之后的操作系统).我相信 SERVICE_CONTROL_PRESHUTDOWN 通知自 Vista 以来已添加,但是当您调用 SetServiceStatus 时,我们是否需要指定:

I write a local service application using C++ and I can't find the correct way of registering for a pre-shut-down notification (for OS later than Windows XP). I believe that SERVICE_CONTROL_PRESHUTDOWN notification has been added since Vista, but when you call SetServiceStatus do we need to specify:

dwServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_PRESHUTDOWN;

dwServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PRESHUTDOWN;

推荐答案

如果您的服务编码正确,则您不能同时接受关闭和预关闭.文档明确说明了这一点.

You cannot accept both a shutdown and a preshutdown if your service is correctly coded. The documentation explicitly states this.

来自 http:///msdn.microsoft.com/en-us/library/windows/desktop/ms683241(v=vs.85).aspx:

参考SERVICE_CONTROL_PRESHUTDOWN:

处理此通知的服务会阻止系统关闭直到服务停止或通过 SERVICE_PRESHUTDOWN_INFO 指定的关闭前超时间隔到期.

A service that handles this notification blocks system shutdown until the service stops or the preshutdown time-out interval specified through SERVICE_PRESHUTDOWN_INFO expires.

在同一页面中,关于SERVICE_CONTROL_SHUTDOWN的部分添加:

In the same page, the section about SERVICE_CONTROL_SHUTDOWN adds:

请注意,注册 SERVICE_CONTROL_PRESHUTDOWN 通知的服务无法接收此通知,因为它们已经停止.

Note that services that register for SERVICE_CONTROL_PRESHUTDOWN notifications cannot receive this notification because they have already stopped.

因此,正确的方法是将 dwControlsAccepted 设置为包含 SERVICE_ACCEPT_SHUTDOWNSERVICE_ACCEPT_PRESHUTDOWN,具体取决于您的需要,但不能同时包含两者同时.

So, the correct way is to set the dwControlsAccepted to include either SERVICE_ACCEPT_SHUTDOWN or SERVICE_ACCEPT_PRESHUTDOWN, depending on your needs, but not to both at the same time.

但请注意,您可能希望接受更多控件.您应该始终至少允许 SERVICE_CONTROL_INTERROGATE,并且几乎可以肯定允许 SERVICE_CONTROL_STOP,因为没有后者,服务无法停止(例如,为了卸载软件)并且进程将不得不被强行终止(即终止).

But do note that you probably want to accept more controls. You should always allow at least SERVICE_CONTROL_INTERROGATE, and almost certainly allow SERVICE_CONTROL_STOP, since without the latter the service cannot be stopped (e.g. in order to uninstall the software) and the process will have to be forcibly terminated (i.e. killed).

这篇关于从 C++ 注册关闭前通知的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆