//
定义一个关机类,定义关机函数
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ZwLylDesktop
... {
class ExitWindows
...{
//声明关机类型
public enum EixtWindowsType
...{
EWX_LOGOFF = 0x00000000,//中止进程,然后注销
EWX_SHUTDOWN = 0x00000001,//关掉系统电源
EWX_REBOOT = 0x00000002,//重新引导系统
EWX_FORCE = 0x00000004,//强迫中止没有响应的进程
EWX_POWEROFF = 0x00000008,
EWX_FORCEIFHUNG = 0x00000010,
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
...{
public int Count;
public long Luid;
public int Attr;
}
//调用Windows API
[DllImport("advapi32.dll")]
private static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess, ref int TokenHandle);
[DllImport("advapi32.dll")]
private static extern int LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll")]
private static extern bool AdjustTokenPrivileges(int htok, bool disall, ref TokPriv1Luid newst, int len, int prev, int relen);
[DllImport("user32.dll")]
private static extern int ExitWindowsEx(EixtWindowsType uFlags, int dwReserved);
[DllImport("kernel32.dll", EntryPoint = "GetCurrentProcess")]
public static extern int GetCurrentProcess();
[DllImport("kernel32.dll", EntryPoint = "GetLastError")]
public static extern int GetLastError();
private const int SE_PRIVILEGE_ENABLED = 0x00000002;
private const int TOKEN_QUERY = 0x00000008;
private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public bool MyExitWindows(EixtWindowsType eixtWindowsType)
...{
TokPriv1Luid tp;
int htok = 0;
int CurrentProcessHandle = GetCurrentProcess();
// Get a token for this process.
if (OpenProcessToken(CurrentProcessHandle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok) == 0)
return false;
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
// Get the LUID for the shutdown privilege
LookupPrivilegeValue(null, "SeShutdownPrivilege", ref tp.Luid);
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(htok, false, ref tp, 0, 0, 0);
if (GetLastError() != 0)
return false;
// Shut down the system and force all applications to close.
if (ExitWindowsEx(eixtWindowsType, 0) == 0)
return false;
return true;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ZwLylDesktop
... {
class ExitWindows
...{
//声明关机类型
public enum EixtWindowsType
...{
EWX_LOGOFF = 0x00000000,//中止进程,然后注销
EWX_SHUTDOWN = 0x00000001,//关掉系统电源
EWX_REBOOT = 0x00000002,//重新引导系统
EWX_FORCE = 0x00000004,//强迫中止没有响应的进程
EWX_POWEROFF = 0x00000008,
EWX_FORCEIFHUNG = 0x00000010,
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
...{
public int Count;
public long Luid;
public int Attr;
}
//调用Windows API
[DllImport("advapi32.dll")]
private static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess, ref int TokenHandle);
[DllImport("advapi32.dll")]
private static extern int LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll")]
private static extern bool AdjustTokenPrivileges(int htok, bool disall, ref TokPriv1Luid newst, int len, int prev, int relen);
[DllImport("user32.dll")]
private static extern int ExitWindowsEx(EixtWindowsType uFlags, int dwReserved);
[DllImport("kernel32.dll", EntryPoint = "GetCurrentProcess")]
public static extern int GetCurrentProcess();
[DllImport("kernel32.dll", EntryPoint = "GetLastError")]
public static extern int GetLastError();
private const int SE_PRIVILEGE_ENABLED = 0x00000002;
private const int TOKEN_QUERY = 0x00000008;
private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public bool MyExitWindows(EixtWindowsType eixtWindowsType)
...{
TokPriv1Luid tp;
int htok = 0;
int CurrentProcessHandle = GetCurrentProcess();
// Get a token for this process.
if (OpenProcessToken(CurrentProcessHandle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok) == 0)
return false;
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
// Get the LUID for the shutdown privilege
LookupPrivilegeValue(null, "SeShutdownPrivilege", ref tp.Luid);
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(htok, false, ref tp, 0, 0, 0);
if (GetLastError() != 0)
return false;
// Shut down the system and force all applications to close.
if (ExitWindowsEx(eixtWindowsType, 0) == 0)
return false;
return true;
}
}
}