如何以编程方式在 Windows (XP|Vista) 中中止关机? [英] How to abort shutdown in Windows (XP|Vista) programatically?

查看:23
本文介绍了如何以编程方式在 Windows (XP|Vista) 中中止关机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够 1. 检测(并且,如果需要,2. 中止)从我的应用程序关闭操作系统,最好使用 Windows API.

I want to be able to 1. detect (and, if needed 2. abort) OS shutdown from my application, preferably by using the Windows API.

我知道可以使用命令 shutdown -a 手动中止关机在最坏的情况下,我可以通过 ShellExecute 执行此操作,但我想知道是否有更好的方法来防止关机以编程方式.

I know that it is possible to abort shutdown manually using the command shutdown -a In the worst case, I could ShellExecute this, but I was wondering if there was a better way to prevent the shutdown programatically.

也许以编程方式通知操作系统即将关闭就足够了 - 如何做到这一点?

Maybe it would be enough to be notified programatically that the OS is about to shut down - how to do this?

推荐答案

来自 MSDN:

WM_QUERYENDSESSION 消息在用户选择结束会话或应用程序调用系统关闭功能之一时发送.如果任何应用程序返回零,则会话不会结束.一旦一个应用程序返回零,系统就会停止发送 WM_QUERYENDSESSION 消息.

The WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls one of the system shutdown functions. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero.

因此,我的应用程序的 WindowProc 现在处理 WM_QUERYENDSESSION 消息并返回 0.

So, my application's WindowProc now processes the WM_QUERYENDSESSION message and returns 0.

没想到这么简单;作为奖励,它也适用于 Windows 2000.

Didn't expect it to be this simple; as a bonus, it also works on Windows 2000.

这篇关于如何以编程方式在 Windows (XP|Vista) 中中止关机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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