2013-07-29 104 views
2

嘗試關閉Windows 7在C#4.0我有一個程序,一些遺留代碼執行以下操作來關閉窗口:System.Management.ManagementException:「未保留特權」。當使用WMI

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上,沒有什麼比已更新這個軟件改變等。

我已經能夠找到,而尋找一個解決方案是一些很老的bug報告有關.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,只需要啓動InitiateSystemShutdownEx或類似的東西?

+1

也許[此鏈接](http://msdn.microsoft.com/en-us/library/windows/desktop/aa393627%28v=vs.85%29.aspx)將有所幫助。 –

+1

你可以使用'右鍵單擊'執行'''以管理員身份運行' – Arshad

+0

Arshad,我試過了,沒有任何改變。 – bj0

回答

2

好的,所以它可能與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調用也是如此。

+0

你對SE_SHUTDOWN_NAME的原始假設是正確的。看看這個[鏈接](http://ithoughthecamewithyou.com/post/reboot-computer-in-c-net)。 它具有啓用該特權的WMI位。您發佈的原始WMI關閉方法仍然是首選,因爲即使用戶未保存工作,它也會強制關閉。 – KWolfe81