使用自定义消息防止 Windows 关闭

发布于 2024-12-06 09:58:25 字数 260 浏览 1 评论 0原文

当我尝试在虚拟机运行时关闭 Windows 时,VMWare Workstation 会执行一些非常酷的操作:

VMWare 使用自定义消息阻止 Windows 关闭

通常,我们会看到“此程序正在阻止 Windows 关闭”消息,而不是新的“1 个虚拟机正在使用中”。

VMWare 是如何做到这一点的?我在 Google 上找不到任何有关它的 API。

VMWare Workstation does something quite cool when I try to shutdown Windows while a Virtual Machine is running:

VMWare prevents Windows shutdown with a custom message

Normally, we'd see a "This program is preventing Windows from shutting down" message instead of the new "1 Virtual Machine is in use".

How does VMWare do this? I haven't been able to find any APIs about it on Google.

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

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

发布评论

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

评论(1

尾戒 2024-12-13 09:58:25

您可以在此处阅读有关 Vista 中引入的所有更改。你真的应该非常仔细地阅读那篇文章。

您正在寻找的 API 是 ShutdownBlockReasonCreate, ShutdownBlockReasonDestroyShutdownBlockReasonQuery。请记住,这些 API 仅在 Vista/2008 服务器上可用。您必须在 2000/XP 上实施回退行为。

如果您需要阻止关闭,请调用 ShutdownBlockReasonCreate 将句柄传递给主窗口,并将原因作为字符串传递。该字符串是关闭阻止对话框中显示的内容,即屏幕截图中的“1 个虚拟机正在使用中”。

如果阻止关闭的操作完成,则调用 ShutdownBlockReasonDestroy

请注意,您仍必须实施 WM_QUERYENDSESSION使所有部分组合在一起。这是实际阻止关闭的部分。

在 XP 上,您还应该回复 WM_ENDSESSION 如果您的应用程序阻止关闭,则有礼貌地显示一条消息说明原因。如果您不这样做,那么用户就会摸不着头脑,不明白为什么计算机会忽略关闭指令。

You can read all about the changes introduced in Vista here. You really should read that article very carefully.

The APIs you are looking for are ShutdownBlockReasonCreate, ShutdownBlockReasonDestroy and ShutdownBlockReasonQuery. Remember that these APIs are only available on Vista/2008 server up. You'll have to implement fall back behaviour on 2000/XP.

If you need to block shutdown you call ShutdownBlockReasonCreate passing the handle to your main window and the reason as a string. This string is what is displayed in the shutdown blocked dialog, i.e. "1 virtual machine is in use" in your screenshot.

If the operation that blocks shutdown completes then you call ShutdownBlockReasonDestroy.

Note that you must still implement WM_QUERYENDSESSION to make all the pieces fit together. This is the part that actually blocks the shutdown.

On XP you should also respond to WM_ENDSESSION and if your app blocked shutdown it is polite to show a message indicating why. If you don't do so then the user is left scratching his/her head as to why the computer is ignoring the instruction to shutdown.

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