gpt4 book ai didi

c# - System.Management.ManagementException : "Privilege not held." When trying to Shutdown Windows 7 in C# 4. 0 使用 WMI

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:16 25 4
gpt4 key购买 nike

我有一个包含一些遗留代码的程序可以执行以下操作来关闭窗口:

ManagementClass mc = new ManagementClass( "Win32_OperatingSystem" );

mc.Get();

mc.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdown = mc.GetMethodParameters( "Win32Shutdown" );

mboShutdown["Flags"] = "5"; // shutdown + force
mboShutdown["Reserved"] = "0";

foreach( ManagementObject mbo in mc.GetInstances() )
{
mbo.InvokeMethod( "Win32Shutdown", mboShutdown, null );
}

这是一个 .NET 3.5 应用程序,可以正常运行。最近,依赖项升级需要将目标框架升级到 4.0 客户端配置文件。现在,每当代码运行时,我都会收到以下异常:

System.Management.ManagementException: "Privilege not held."

该应用程序在 Windows 7 的管理员帐户下运行,除了更新此软件外没有任何变化。

在搜索解决方案时,我能够找到的唯一信息是一些关于 .NET 1.1 的非常古老的错误报告,以及 msdn 上的以下线程从未得到答复: http://social.msdn.microsoft.com/Forums/vstudio/en-US/fa0bcae5-6f30-42b6-bb5f-b8a6edb88ac4/encountered-privillege-not-held-exception-when-rebooting-the-server-in-net40-framewrk

有谁知道这个问题的原因是什么?我是否需要停止使用 WMI 而只使用 PInvoke InitiateSystemShutdownEx 或类似的东西?

最佳答案

好的,所以它可能与 SE_SHUTDOWN_NAME 权限有关。我不确定为什么它在 .NET 3.5 而不是 .NET 4.0 下工作,但以下解决方法有效:

[StructLayout( LayoutKind.Sequential, Pack = 1 )]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

[DllImport( "kernel32.dll", ExactSpelling = true )]
internal static extern IntPtr GetCurrentProcess();

[DllImport( "advapi32.dll", ExactSpelling = true, SetLastError = true )]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );

[DllImport( "advapi32.dll", SetLastError = true )]
internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );

[DllImport( "advapi32.dll", ExactSpelling = true, SetLastError = true )]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );

[DllImport( "user32.dll", ExactSpelling = true, SetLastError = true )]
internal static extern bool ExitWindowsEx( int flg, int rea );

public const int SE_PRIVILEGE_ENABLED = 0x00000002;
public const int TOKEN_QUERY = 0x00000008;
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
public const int EWX_LOGOFF = 0x00000000;
public const int EWX_SHUTDOWN = 0x00000001;
public const int EWX_REBOOT = 0x00000002;
public const int EWX_FORCE = 0x00000004;
public const int EWX_POWEROFF = 0x00000008;
public const int EWX_FORCEIFHUNG = 0x00000010;


public static bool DoExitWin( int flg )
{
TokPriv1Luid tp;
var hproc = GetCurrentProcess();
var htok = IntPtr.Zero;
OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );

return ExitWindowsEx( flg, 0 );
}

我没有尝试过,但我猜测 WMI 调用在使用 AdjustTokenPrivileges 调用后也可能会起作用。

关于c# - System.Management.ManagementException : "Privilege not held." When trying to Shutdown Windows 7 in C# 4. 0 使用 WMI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17936210/

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