#region
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
#endregion
namespace CSFrameworkV5.Common
{
///
/// 控制计算机关闭、重新启动、注销
///
public class ComputerShutDown
{
private const int SE_PRIVILEGE_ENABLED = 0x00000002;
private const int TOKEN_QUERY = 0x00000008;
private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
private const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
private const int EWX_LOGOFF = 0x00000000;
private const int EWX_SHUTDOWN = 0x00000001;
private const int EWX_REBOOT = 0x00000002;
private const int EWX_FORCE = 0x00000004;
private const int EWX_POWEROFF = 0x00000008;
private const int EWX_FORCEIFHUNG = 0x00000010;
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool AdjustTokenPrivileges(IntPtr htok,
bool disall, ref TokPriv1Luid newst, int len,
IntPtr prev, IntPtr relen);
///
/// 关闭计算机
///
public static void Close()
{
//创建访问控制本地系统进程的对象实例
var myprocess = new Process();
myprocess.StartInfo.FileName = "cmd.exe";
myprocess.StartInfo.UseShellExecute = false;
myprocess.StartInfo.RedirectStandardInput = true;
myprocess.StartInfo.RedirectStandardOutput = true;
myprocess.StartInfo.RedirectStandardError = true;
myprocess.StartInfo.CreateNoWindow = true;
myprocess.Start();
myprocess.StandardInput.WriteLine("shutdown -s -t 0");
}
private static void DoExitWin(int DoFlag)
{
bool ok;
TokPriv1Luid tp;
var hproc = GetCurrentProcess();
var htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero,
IntPtr.Zero);
var i = ExitWindowsEx(DoFlag, 0);
}
//设置注销、关闭、重新启动计算机参数
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx",
CharSet = CharSet.Ansi)]
private static extern int ExitWindowsEx(int uFlags, int dwReserved);
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetCurrentProcess();
///
/// 注销用户
///
public static void Logout()
{
ExitWindowsEx(0, 0);
}
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LookupPrivilegeValue(string host,
string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr h, int acc,
ref IntPtr phtok);
///
/// 重新启动计算机
///
public static void Restart()
{
//创建访问控制本地系统进程的对象实例
var myprocess = new Process();
myprocess.StartInfo.FileName = "cmd.exe";
myprocess.StartInfo.UseShellExecute = false;
myprocess.StartInfo.RedirectStandardInput = true;
myprocess.StartInfo.RedirectStandardOutput = true;
myprocess.StartInfo.RedirectStandardError = true;
myprocess.StartInfo.CreateNoWindow = true;
myprocess.Start();
myprocess.StandardInput.WriteLine("shutdown -r -t 0");
}
///
/// 注销用户
///
public static void Win32_LogOff()
{
DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
///
/// 关闭电源,关闭计算机
///
public static void Win32_PowerOff()
{
DoExitWin(EWX_FORCE | EWX_POWEROFF);
}
///
/// 重新启动计算机
///
public static void Win32_Reboot()
{
DoExitWin(EWX_FORCE | EWX_REBOOT);
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
}
}