/// <summary> /// api delcare /// </summary> public const int ANYSIZE_ARRAY = 1; public const int TOKEN_QUERY = 0x0008; public const int TOKEN_QUERY_SOURCE = 0x0010; public const int TOKEN_ADJUST_PRIVILEGES = 0x0020; public const int SE_PRIVILEGE_ENABLED = 0x00000002; public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; public const int ERROR_SUCCESS = 0; public const int EWX_POWEROFF = 0x00000008; public const int EWX_FORCE = 0x00000004; [StructLayout(LayoutKind.Sequential)] public struct TOKEN_PRIVILEGES { public int PrivilegeCount; public LUID_AND_ATTRIBUTES[] Privileges; public TOKEN_PRIVILEGES(int i) { PrivilegeCount = 0; Privileges = new LUID_AND_ATTRIBUTES[ANYSIZE_ARRAY]; } } [StructLayout(LayoutKind.Sequential)] public struct LUID_AND_ATTRIBUTES { public LUID pLuid; public int Attributes; } [StructLayout(LayoutKind.Sequential)] public struct LUID { public int LowPart; public int HighPart; } [DllImport("advapi32.dll", EntryPoint="OpenProcessToken")] public static extern int OpenProcessToken ( int ProcessHandle, int DesiredAccess, ref int TokenHandle ); [DllImport("kernel32.dll", EntryPoint="GetCurrentProcess")] public static extern int GetCurrentProcess (); [DllImport("advapi32.dll", EntryPoint="LookupPrivilegeValue")] public static extern int LookupPrivilegeValue ( string lpSystemName, string lpName, ref LUID lpLuid ); [DllImport("advapi32.dll", EntryPoint="AdjustTokenPrivileges")] public static extern int AdjustTokenPrivileges ( int TokenHandle, int DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, int BufferLength, ref TOKEN_PRIVILEGES PreviousState, ref int ReturnLength ); [DllImport("kernel32.dll", EntryPoint="GetLastError")] public static extern int GetLastError (); [DllImport("user32.dll", EntryPoint="ExitWindowsEx")] public static extern int ExitWindowsEx ( int uFlags, int dwReserved ); private bool myExitWindow() { int hToken; TOKEN_PRIVILEGES tkp; // Get a token for this process. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref hToken) == 0) return false ; // Get the LUID for the shutdown privilege. LookupPrivilegeValue("", SE_SHUTDOWN_NAME, ref tkp.Privileges[0].pLuid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. int i = 0; AdjustTokenPrivileges(hToken, 0, ref tkp, 0, ref tkp, ref i); if (GetLastError() != ERROR_SUCCESS) return false; // Shut down the system and force all applications to close. if (ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0) == 0) // if (!ExitWindowsEx(EWX_POWEROFF | EWX_FORCEIFHUNG, 0)) return false; return true; } }
process.start("cmd.exe","shutdowm /s /t 0");
里面有一个 s h u t d o w n
public class 关机 { public enum EixtWindowsType { EWX_LOGOFF=0x00000000,//中止进程,然后注销 EWX_SHUTDOWN=0x00000001,//关掉系统电源(如果可能的话,ATX电源就可以) 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; } [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); private const int SE_PRIVILEGE_ENABLED = 0x00000002; private const int TOKEN_QUERY = 0x00000008; private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; public static void ExitWindows(EixtWindowsType eixtWindowsType) { TokPriv1Luid tp; int CurrentProcessHandle = Socg.API.API.ProcessThread.GetCurrentProcess(); int htok = 0; OpenProcessToken(CurrentProcessHandle,TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,ref htok); tp.Count = 1; tp.Luid = 0; tp.Attr = SE_PRIVILEGE_ENABLED; LookupPrivilegeValue(null,"SeShutdownPrivilege",ref tp.Luid); AdjustTokenPrivileges(htok,false,ref tp,0,0,0); ExitWindowsEx(eixtWindowsType,0); }}
namespace ShutDownTest { using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using System.Security.Principal; using System.Threading; [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] public struct LUID { public uint LowPart; public uint HighPart; }; [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] public struct TOKEN_PRIVILEGES { public uint PrivilegeCount; public LUID Luid; public uint Attributes; }; public class PlatformImport { [DllImport("Advapi32.dll", CharSet=CharSet.Auto)] public static extern bool LookupPrivilegeValue (string sysname,string privname,ref LUID luid); [DllImport("advapi32", CharSet=CharSet.Auto)] public static extern bool AdjustTokenPrivileges(IntPtr handle, bool dsall,ref TOKEN_PRIVILEGES newstate,int len, IntPtr oldstate,IntPtr retlen); [DllImport("kernel32.dll")] public static extern int GetLastError(); [DllImport("user32.dll")] public static extern bool ExitWindowsEx(int uFlags, int dwReason); } public class Form1 : Form { private Button button1; private TextBox textBox1; public Form1() { this.textBox1=new TextBox(); this.textBox1.Location=new Point(10,10); this.Controls.Add(this.textBox1);
// this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new Point(136, 112);
this.button1.Name = "button1";
this.button1.Size = new Size(80, 24);
this.button1.TabIndex = 0;
this.button1.Text = "Shut Down";
this.button1.Click += new EventHandler(this.button1_Click);
/// api delcare
/// </summary>
public const int ANYSIZE_ARRAY = 1;
public const int TOKEN_QUERY = 0x0008;
public const int TOKEN_QUERY_SOURCE = 0x0010;
public const int TOKEN_ADJUST_PRIVILEGES = 0x0020;
public const int SE_PRIVILEGE_ENABLED = 0x00000002;
public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
public const int ERROR_SUCCESS = 0;
public const int EWX_POWEROFF = 0x00000008;
public const int EWX_FORCE = 0x00000004; [StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public LUID_AND_ATTRIBUTES[] Privileges;
public TOKEN_PRIVILEGES(int i)
{
PrivilegeCount = 0;
Privileges = new LUID_AND_ATTRIBUTES[ANYSIZE_ARRAY];
}
} [StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
public LUID pLuid;
public int Attributes;
} [StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public int LowPart;
public int HighPart;
} [DllImport("advapi32.dll", EntryPoint="OpenProcessToken")]
public static extern int OpenProcessToken (
int ProcessHandle,
int DesiredAccess,
ref int TokenHandle
); [DllImport("kernel32.dll", EntryPoint="GetCurrentProcess")]
public static extern int GetCurrentProcess (); [DllImport("advapi32.dll", EntryPoint="LookupPrivilegeValue")]
public static extern int LookupPrivilegeValue (
string lpSystemName,
string lpName,
ref LUID lpLuid
); [DllImport("advapi32.dll", EntryPoint="AdjustTokenPrivileges")]
public static extern int AdjustTokenPrivileges (
int TokenHandle,
int DisableAllPrivileges,
ref TOKEN_PRIVILEGES NewState,
int BufferLength,
ref TOKEN_PRIVILEGES PreviousState,
ref int ReturnLength
); [DllImport("kernel32.dll", EntryPoint="GetLastError")]
public static extern int GetLastError (); [DllImport("user32.dll", EntryPoint="ExitWindowsEx")]
public static extern int ExitWindowsEx (
int uFlags,
int dwReserved
); private bool myExitWindow()
{
int hToken;
TOKEN_PRIVILEGES tkp; // Get a token for this process. if (OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref hToken) == 0)
return false ; // Get the LUID for the shutdown privilege. LookupPrivilegeValue("", SE_SHUTDOWN_NAME,
ref tkp.Privileges[0].pLuid); tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. int i = 0;
AdjustTokenPrivileges(hToken, 0, ref tkp, 0,
ref tkp, ref i); if (GetLastError() != ERROR_SUCCESS)
return false; // Shut down the system and force all applications to close. if (ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0) == 0)
// if (!ExitWindowsEx(EWX_POWEROFF | EWX_FORCEIFHUNG, 0))
return false; return true;
}
}
{
public enum EixtWindowsType
{
EWX_LOGOFF=0x00000000,//中止进程,然后注销
EWX_SHUTDOWN=0x00000001,//关掉系统电源(如果可能的话,ATX电源就可以)
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;
}
[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); private const int SE_PRIVILEGE_ENABLED = 0x00000002;
private const int TOKEN_QUERY = 0x00000008;
private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; public static void ExitWindows(EixtWindowsType eixtWindowsType)
{
TokPriv1Luid tp;
int CurrentProcessHandle = Socg.API.API.ProcessThread.GetCurrentProcess();
int htok = 0;
OpenProcessToken(CurrentProcessHandle,TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue(null,"SeShutdownPrivilege",ref tp.Luid);
AdjustTokenPrivileges(htok,false,ref tp,0,0,0);
ExitWindowsEx(eixtWindowsType,0);
}}
{
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using System.Security.Principal;
using System.Threading;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] public struct LUID { public uint LowPart; public uint HighPart; };
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] public struct TOKEN_PRIVILEGES { public uint PrivilegeCount; public LUID Luid; public uint Attributes; };
public class PlatformImport { [DllImport("Advapi32.dll", CharSet=CharSet.Auto)] public static extern bool LookupPrivilegeValue (string sysname,string privname,ref LUID luid);
[DllImport("advapi32", CharSet=CharSet.Auto)] public static extern bool AdjustTokenPrivileges(IntPtr handle, bool dsall,ref TOKEN_PRIVILEGES newstate,int len, IntPtr oldstate,IntPtr retlen);
[DllImport("kernel32.dll")] public static extern int GetLastError(); [DllImport("user32.dll")] public static extern bool ExitWindowsEx(int uFlags, int dwReason); }
public class Form1 : Form {
private Button button1;
private TextBox textBox1;
public Form1()
{
this.textBox1=new TextBox();
this.textBox1.Location=new Point(10,10);
this.Controls.Add(this.textBox1);
//
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new Point(136, 112);
this.button1.Name = "button1";
this.button1.Size = new Size(80, 24);
this.button1.TabIndex = 0;
this.button1.Text = "Shut Down";
this.button1.Click += new EventHandler(this.button1_Click);
this.AutoScaleBaseSize = new Size(6, 14);
this.ClientSize = new Size(292, 273);
this.Controls.Add(button1);
this.Name = "Form1";
this.Text = "shutdown test";
this.ResumeLayout(false);
}
[STAThread] static void Main() {
Application.Run(new Form1()); }
private void button1_Click(object sender, System.EventArgs e) {
// Thread.Sleep(this.MinToMs(int.Parse(this.textBox1.Text)));
// Thread.Sleep(10000);
// MessageBox.Show("10second");
Thread myThr=new Thread(new ThreadStart(CloseComputer));
myThr.Start();
/*
*/ } private int MinToMs(int min)//分钟换算成毫秒
{
return min*1000*60;
} private void CloseComputer()
{
Thread.Sleep(this.MinToMs(int.Parse(this.textBox1.Text)));
WindowsIdentity identity = WindowsIdentity.GetCurrent();
IntPtr token = identity.Token;
IntPtr luid = (IntPtr)0;
IntPtr previousState = (IntPtr)0;
IntPtr previousStateLength = (IntPtr)0;
LUID privilegeId = new LUID ();
PlatformImport.LookupPrivilegeValue("", "SeShutdownPrivilege", ref privilegeId);
TOKEN_PRIVILEGES privileges = new TOKEN_PRIVILEGES();
privileges.PrivilegeCount = 1;
privileges.Luid = privilegeId;
privileges.Attributes = 2;
PlatformImport.AdjustTokenPrivileges (token, false, ref privileges, Marshal.SizeOf(privileges), previousState, previousStateLength);
if (PlatformImport.GetLastError() != 0) return;
if (!(PlatformImport.ExitWindowsEx(8, 0))) return; } } }