Process process = new Process(); process.StartInfo.FileName = "shutdown.exe"; process.StartInfo.Arguments = "-s -t 0"; process.Start();
[DllImport("user32.dll")] private static extern int ExitWindowsEx(int x,int y); ExitWindowsEx(1,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 DoFlag, int rea); internal const int SE_PRIVILEGE_ENABLED = 0x00000002; internal const int TOKEN_QUERY = 0x00000008; internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; internal const int EWX_LOGOFF = 0x00000000; internal const int EWX_SHUTDOWN = 0x00000001; internal const int EWX_REBOOT = 0x00000002; internal const int EWX_FORCE = 0x00000004; internal const int EWX_POWEROFF = 0x00000008; internal const int EWX_FORCEIFHUNG = 0x00000010; private static void DoExitWin(int DoFlag) { bool ok; TokPriv1Luid tp; IntPtr hproc = GetCurrentProcess(); IntPtr 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); ok = ExitWindowsEx(DoFlag, 0); } DoExitWin(EWX_FORCE | EWX_POWEROFF);
/// <summary> 获得关机所需要的权限</summary> /// <param name="Privilege">权限类型</param> [DllImport("ntdll.dll")] private static extern void RtlAdjustPrivilege(int Privilege, int NewValue, int NewThread, out bool OldValue); /// <summary>关闭计算机权限</summary> const int SE_SHUTDOWN_PRIVILEGE = 19; /// <summary>执行瞬间关机等操作</summary> /// <param name="ShutdownAction">关机动作</param> [DllImport("ntdll.dll")] private static extern void NtShutdownSystem(int ShutdownAction); /// <summary>关闭系统</summary> const int ShutDown = 0; /// <summary>重启系统</summary> const int RESTART = 1; /// <summary>切断电源(快速关机)</summary> const int POWEROFF = 2; /// <summary>关机操作 </summary> /// <param name="Index">操作类型:1关机、2重启动、3切断电源(快速关机)</param> private void GuJi(int Index) { bool a; RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, 1, 0, out a); //取得关机权限 switch (Index) { case ShutDown://关机 NtShutdownSystem(ShutDown); break; case RESTART: //重启动 NtShutdownSystem(RESTART); break; case POWEROFF: //切断电源(快速关机) NtShutdownSystem(POWEROFF); break; } } 调用: GuJi(POWEROFF);//快速关机
我这里有个C++的 XP系统关机函数,不知道对你有用没 bool ShutDown() { HANDLE hToken; TOKEN_PRIVILEGES tkp; //Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken)) { error("OpenProcessToken failed!"); return false; } //Get the LUID for the shutdown privilege. LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; //one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; //Get the shutdown privilege for this process. AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0); //Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) { error("AdjustTokenPrivileges failed!"); return false; } /* if(!InitiateSystemShutdown(NULL,"系统即将关闭。",20,false,false)) error("InitiateSystemShutdown failed."); */ //Shut down the system and force all applications to close. if (!ExitWindowsEx(EWX_POWEROFF | EWX_FORCE,0)) { error("ExitWindowsEx failed!"); return false; } return true; }
process.StartInfo.FileName = "shutdown.exe";
process.StartInfo.Arguments = "-s -t 0";
process.Start();
private static extern int ExitWindowsEx(int x,int y); ExitWindowsEx(1,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 DoFlag, int rea);
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;
private static void DoExitWin(int DoFlag)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr 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);
ok = ExitWindowsEx(DoFlag, 0);
} DoExitWin(EWX_FORCE | EWX_POWEROFF);
/// <summary> 获得关机所需要的权限</summary>
/// <param name="Privilege">权限类型</param>
[DllImport("ntdll.dll")]
private static extern void RtlAdjustPrivilege(int Privilege, int NewValue, int NewThread, out bool OldValue);
/// <summary>关闭计算机权限</summary>
const int SE_SHUTDOWN_PRIVILEGE = 19; /// <summary>执行瞬间关机等操作</summary>
/// <param name="ShutdownAction">关机动作</param>
[DllImport("ntdll.dll")]
private static extern void NtShutdownSystem(int ShutdownAction);
/// <summary>关闭系统</summary>
const int ShutDown = 0;
/// <summary>重启系统</summary>
const int RESTART = 1;
/// <summary>切断电源(快速关机)</summary>
const int POWEROFF = 2; /// <summary>关机操作 </summary>
/// <param name="Index">操作类型:1关机、2重启动、3切断电源(快速关机)</param>
private void GuJi(int Index)
{
bool a;
RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, 1, 0, out a); //取得关机权限
switch (Index)
{
case ShutDown://关机
NtShutdownSystem(ShutDown);
break;
case RESTART: //重启动
NtShutdownSystem(RESTART);
break;
case POWEROFF: //切断电源(快速关机)
NtShutdownSystem(POWEROFF);
break;
}
} 调用:
GuJi(POWEROFF);//快速关机
bool ShutDown()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
//Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken)) {
error("OpenProcessToken failed!");
return false;
} //Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; //one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; //Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0); //Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
{
error("AdjustTokenPrivileges failed!");
return false;
}
/*
if(!InitiateSystemShutdown(NULL,"系统即将关闭。",20,false,false))
error("InitiateSystemShutdown failed.");
*/
//Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_POWEROFF | EWX_FORCE,0))
{
error("ExitWindowsEx failed!");
return false;
}
return true;
}