如何捕获从 Windows 服务启动的会话注销

发布于 2024-11-05 14:57:47 字数 373 浏览 1 评论 0原文

我有一项服务不断检查某些应用程序并确保它没有关闭。如果此应用程序关闭 - 服务会再次启动它。

当用户决定注销会话时,问题就开始了。注销期间,所有应用程序都将关闭,包括我的应用程序。但该服务仍在运行并不断尝试重新启动。

问题是如何通知服务用户将要注销并且应用程序不再需要重新启动?我尝试使用 SERVICE_CONTROL_SESSIONCHANGE 通知来实现它。但根据 MSDN 的说法,当所有应用程序都已关闭且注销程序已完成时,它们就会开始提供服务。对我来说已经太晚了。有没有什么方法可以以编程方式找出当前会话正在注销?

我的服务在 LocalSystem 帐户下启动。

谢谢。

ps 我无权访问应用程序源代码。需要在不修改目标的情况下实现目标。

I have a service which constantly checks some application and assures that it wasn't closed. If this app closed - service launches it again.

The problem starts when user decides to log off the session. During logoff all applications are closing including the mine one. But the service is still running and constantly trying to start it again.

The questions is how to notify the service that user is going to log off and the application doesn't need to be restarted anymore? I've tried to make it using SERVICE_CONTROL_SESSIONCHANGE notification. But in accordance to MSDN they come to services when all apps already closed and logging off procedure completed. It is too late for me. Is there any way to programmatically find out that current session is in process of logging off?

My service launched under LocalSystem account.

Thanks.

p.s. I don't have the access to application source code. The goal need to be achieved without modifying it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

楠木可依 2024-11-12 14:57:47

让您的服务运行两个应用程序:一个是它的看门狗,另一个是您自己实现的应用程序。然后,第二个可以通过向服务发送消息来响应注销事件(按照 David Heffernan 的答案),然后服务将知道不要重新启动监视的作业。

Have your service run two applications: the one it is a watchdog for, and a second one which you implement yourself. This second one can then respond to the log-off event by sending a message to the service (a la David Heffernan's answer), and the service will then know not to restart the watched job.

☆獨立☆ 2024-11-12 14:57:47

打开您的应用程序和服务之间的通信通道,并安排应用程序告诉服务它由于注销事件而关闭。

Open a communication channel between your app and your service and arrange for the app to tell the service that it is closing because of a logoff event.

傾旎 2024-11-12 14:57:47

您可能无权访问可执行文件的源代码,但这并不意味着您不能影响进程。例如,您可以注入 DLL。使用 SetWindowsHookEx,您可以捕获发送到应用主窗口的 WM_ENDSESSION

You may not have access to the source code of the executable, but that doesn't mean that you can't affect the process. For instance, you could inject a DLL. Using SetWindowsHookEx, you'd catch the WM_ENDSESSION sent to the apps main window.

请止步禁区 2024-11-12 14:57:47

为什么不在运行应用程序的用户下创建服务并告诉它自动启动?在这种情况下,当您的用户注销时,您应该收到 SERVICE_CONTROL_SHUTDOWN 消息,因为服务也会终止。

Why don't you create your service under the user that is running the App and tell it to startup automatic? In this case you should get the SERVICE_CONTROL_SHUTDOWN message when your user is logging off since the service would also be terminated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文