gpt4 book ai didi

c# - 调用 'ShutdownBlockReasonCreate'函数不能阻止用户关闭系统

转载 作者:行者123 更新时间:2023-11-30 14:45:10 24 4
gpt4 key购买 nike

This文章说:

If an application must block a potential system shutdown, it can call the ShutdownBlockReasonCreate function. The caller provides a reason string that will be displayed to the user.

并且在 ShutdownBlockReasonCreate documentation 它清楚地表明在尝试关闭时将向用户显示一个带有原因字符串的对话框窗口:

Indicates that the system cannot be shut down and sets a reason string to be displayed to the user if system shutdown is initiated

并且在 this 中确认了该对话窗口的外观讨论:

The user could click "Shut down anyway". Besides, the system assumes "Shut down anyway" if the user takes no action within some number of seconds.

但是,在我调用 ShutdownBlockReasonCreate 并传递当前应用程序的主窗口句柄后,确保该函数成功并通过调用 ShutdownBlockReasonQuery 双重确保它。函数来检索原因字符串,它不会阻止用户关闭系统并且不会显示对话窗口。

为什么它对我的系统没有影响?我该如何解决这个问题?。

我使用管理员(内置)帐户在 Windows 10 x64 上运行,我使用的代码来自 this GitHub 存储库:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Vanara.PInvoke;
using static Vanara.PInvoke.User32;

namespace Vanara.Windows.Forms.Forms
{
/// <summary>Used to define a set of operations within which any shutdown request will be met with a reason why this application is blocking it.</summary>
/// <remarks>This is to be used in either a 'using' statement or for the life of the application.
/// <para>To use for the life of the form, define a class field:
public class PreventShutdownContext : IDisposable
{
private HandleRef href;

/// <summary>Initializes a new instance of the <see cref="PreventShutdownContext"/> class.</summary>
/// <param name="window">The <see cref="Form"/> or <see cref="Control"/> that contains a valid window handle.</param>
/// <param name="reason">The reason the application must block system shutdown. Because users are typically in a hurry when shutting down the system, they may spend only a few seconds looking at the shutdown reasons that are displayed by the system. Therefore, it is important that your reason strings are short and clear.</param>
public PreventShutdownContext(Control window, string reason)
{
href = new HandleRef(window, window.Handle);
Reason = reason;
}

/// <summary>The reason the application must block system shutdown. Because users are typically in a hurry when shutting down the system, they may spend only a few seconds looking at the shutdown reasons that are displayed by the system. Therefore, it is important that your reason strings are short and clear.</summary>
/// <value>The reason string.</value>
public string Reason
{
get
{
if (!ShutdownBlockReasonQuery(href.Handle, out var reason))
Win32Error.ThrowLastError();
return reason;
}
set
{
if (value == null) value = string.Empty;
if (ShutdownBlockReasonQuery(href.Handle, out var _))
ShutdownBlockReasonDestroy(href.Handle);
if (!ShutdownBlockReasonCreate(href.Handle, value))
Win32Error.ThrowLastError();
}
}

/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
ShutdownBlockReasonDestroy(href.Handle);
}
}
}

...

[DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShutdownBlockReasonCreate(HWND hWnd, [MarshalAs(UnmanagedType.LPWStr)] string reason);

[DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShutdownBlockReasonQuery(HWND hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszBuff, ref uint pcchBuff);

[DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShutdownBlockReasonDestroy(HWND hWnd);

像这样的用法:

using (new PreventShutdownContext(this, "This app is super busy right now."))
{
// Do something that can't be interrupted...
}

我按原样尝试了代码,使用它的 P/Invoke 定义,还对我使用 IntPtr 结构而不是自定义的窗口句柄的代码做了一些修改 HWND结构,并将我在上面的评论中指定的应用程序的主窗口句柄传递给它。

最佳答案

这是设计使然。

documentation (以及您引用的主题)可能会产生轻微的误导。

Indicates that the system cannot be shut down and sets a reason string to be displayed to the user if system shutdown is initiated.

If an application must block a potential system shutdown, it can call the ShutdownBlockReasonCreate function.

这个函数实际上只是为您的应用程序设置消息字符串。此函数不会阻止您的应用程序被关闭。

要实现关闭 block ,只需按照您引用的文章中描述的步骤操作即可。您需要对 WM_QUERYENDSESSION 消息使用react并返回 FALSE (0)。作为引用,另请参阅 WM_QUERYENDSESSION文档。

您可能还会发现有趣的 this topic - 它描述了 Windows Vista 引入的变化,并包含如何实现关闭逻辑的最佳实践。

顺便说一句,关于您的应用程序不会有特殊的“对话窗口”。将显示标准的 Windows 关机 UI(它因操作系统版本而异)。您的应用程序将出现在“阻止关闭的应用程序”列表中,其中包含您使用 ShutdownBlockReasonCreate 函数注册的消息 - 但前提是它返回 FALSE 用于 WM_QUERYENDSESSION 消息。


更新

如果上述解决方案(WM_QUERYENDSESSION)没有解决问题,可能是系统设置忽略了这个机制。

正如@ElektroStudios 在他们的研究中发现的那样:

  • 如果用户设置了 AutoEndTasks 注册表值(在 HKCU\Control Panel\Desktop 注册表项中找到),则关机不会显示任何 UI 让用户取消关机。所以在这些情况下创建“取消关闭原因”是没有用的,因为应用程序无论如何都会被强制立即关闭(以继续关闭)。作为引用,请阅读 this MS Docs topic .
  • 为了使这个东西按预期工作,AutoEndTasks 注册表值必须为 0(零);否则,任何试图阻止关机的应用程序都将被终止,并且在关机时不会显示任何用户界面。
  • AutoEndTasks 值可以添加到 HKEY_USERS\.DEFAULT\Control Panel\Desktop 键,该键覆盖 HKCU 中定义的值配置单元和 HKU\{SID}。这意味着,如果 AutoEndTasksHKCU 中为 false (0) 但在 中为 true (1) HKU\.DEFAULT,则应用不会阻止系统关机,也不会显示关机界面。如果 AutoEndTasksHKU\.DEFAULT 中为 false 但在 HKCU 中为 true,则应用将阻止系统从关闭和关闭 UI 将显示。
  • 还有一个好处是 AutoEndTasks 值不需要重新启动/注销系统即可生效。因此,一旦在适当的键中将其设置为 false(例如 HKEY_USERS\.DEFAULT\Control Panel\Desktop),应用程序将阻止系统关闭,我们当我们完成使用该功能时,可以将该值恢复到之前的状态。

关于c# - 调用 'ShutdownBlockReasonCreate'函数不能阻止用户关闭系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54842853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com