使用自定义消息防止 Windows 关闭
当我尝试在虚拟机运行时关闭 Windows 时,VMWare Workstation 会执行一些非常酷的操作:
通常,我们会看到“此程序正在阻止 Windows 关闭”消息,而不是新的“1 个虚拟机正在使用中”。
VMWare 是如何做到这一点的?我在 Google 上找不到任何有关它的 API。
VMWare Workstation does something quite cool when I try to shutdown Windows while a Virtual Machine is running:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在此处阅读有关 Vista 中引入的所有更改。你真的应该非常仔细地阅读那篇文章。
您正在寻找的 API 是
ShutdownBlockReasonCreate
,
ShutdownBlockReasonDestroy
和ShutdownBlockReasonQuery
。请记住,这些 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
andShutdownBlockReasonQuery
. 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.