当用户发起注销/关闭操作时停止应用程序

发布于 2024-12-14 07:04:11 字数 589 浏览 4 评论 0 原文

我有一个同事提供的这个应用程序(用 Visual C++ 编写的 Windows 窗体应用程序),但我面临一些严重的问题。该应用程序既不是服务也不是普通应用程序,我的意思是它有一个 GUI,但大多数时候它在后台运行(它应该作为服务进行反应,但事实并非如此)。该应用程序阻止用户注销,我需要能够做到这一点。

我知道当用户尝试注销时,Windows 会向所有正在运行的应用程序发送 WM_QUERYENDSESSION 消息。我试图在我的 WndProc() 函数上捕获此消息并强制终止应用程序,但它只能运行一次。当我再次登录并尝试注销时,操作不会终止,因为我的应用程序不会关闭。

如果我尝试使用 SessionEnding 事件,应用程序只会放入系统托盘并保留在那里而不会注销,但我相信这是因为 Form_Closing 方法执行此操作关闭程序(这是要求,我无法更改)。

也许另一个有用的信息是应用程序会自动启动,因为它在 HKLM 注册表中有一个条目,并且该应用程序始终有 2 个实例在运行(一个应该监督另一个实例并在崩溃时重新启动它)但在“手动”关闭的情况下则不然)。

任何建议都会受到热烈欢迎。

谢谢。

I have this application (Windows form application written in Visual C++) from a colleague of mine and I face some serious problems. The application is neither a service neither a normal application, I mean it has a GUI but most of the time it runs in background (it should react as a service but it's not). This application is preventing the user to log off, and I need to be able to do that.

I know that Windows sends the WM_QUERYENDSESSION message to all running applications when the user tries to log off. I have tried to catch this message on my WndProc() function and to kill the application by force but it only works once. When I login again and try to log off the operation is not terminated because my application won't close.

If I try to use SessionEnding event, the application is only put to the system tray and remains there without logging off but this I believe it's because the Form_Closing method performs this operation instead of closing the program (this was the requirement and I can't change that).

Maybe another useful information is that the application starts automatically because it has an entry in the HKLM registry and there are always 2 instances of this application running (one should supervise the other one and restart it in case of crash but not in case of "manually" shut down).

Any suggestions will be well received.

Thanks.

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

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

发布评论

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

评论(3

想你的星星会说话 2024-12-21 07:04:11

是的,这个问题是由表单的 FormClosing 事件处理程序引起的。您必须注意关闭原因,并且在用户关闭窗口时取消它。像这样:

    System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
        // Do this *only* when the user closes the window
        if (e->CloseReason == CloseReason::UserClosing) {
            e->Cancel = true;
            this->Hide();
            // etc...
        }
    }

Yes, this problem is caused by the form's FormClosing event handler. You have to pay attention to the close reason and only cancel it when the user is closing the window. Like this:

    System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
        // Do this *only* when the user closes the window
        if (e->CloseReason == CloseReason::UserClosing) {
            e->Cancel = true;
            this->Hide();
            // etc...
        }
    }
┾廆蒐ゝ 2024-12-21 07:04:11

SetConsoleCtrlHandler 可能提供解决方案,因为当用户注销和系统关闭时会生成事件。

SetConsoleCtrlHandler may provide the solution as events are generated when the user logs off and when the system shuts down.

煮酒 2024-12-21 07:04:11

如果您可以控制源代码,请将第二个(控制器)实例重新实现为真正的 Windows 服务。让它监视 GUI 实例,并在失败或需要关闭时执行所需的控制操作。

这免费为您提供一些 自动重启逻辑,您当前必须手动执行,以及允许通过SERVICE_CONTROL_SHUTDOWN

If you have control of the source code, reimplement the second (controller) instance as a true Windows service. Have this monitor the GUI instance and perform required control actions if it fails or shutdown is needed.

This gives you for free some of the auto-restart logic that you currently have to do manually, as well as allowing proper shutdown handling via SERVICE_CONTROL_SHUTDOWN

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