1

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.

4

3 回答 3

1

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

于 2011-11-08T12:52:37.593 回答
1

是的,这个问题是由表单的 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...
        }
    }
于 2011-11-08T13:30:01.397 回答
0

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

这为您免费提供了一些您当前必须手动执行的自动重启逻辑,并允许通过SERVICE_CONTROL_SHUTDOWN进行正确的关机处理

于 2011-11-08T12:52:34.533 回答