请教用C#如何来控制系统关机
问题是这样的,, 我已经将系统的关机给屏蔽了,,,但我要关机,所以我用C#自制一个关机程序.
请问我怎样才能获取系统的关机的程序呢??在 win 2000 /xp /2003 三种系统下面.
public enum ExitWindows : uint
{
LogOff = 0x00,
ShutDown = 0x01,
Reboot = 0x02,
Force = 0x04,
PowerOff = 0x08,
ForceIfHung = 0x10
}
[DllImport("user32.dll")]
static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);
[STAThread]
static void Main(string[] args)
{
ExitWindowsEx(ExitWindows.LogOff, ShutdownReason.MajorOther & ShutdownReason.MinorOther);
//this will cause the computer to logoff.
}
在2000下的话需要提升关机权限
using System;
using System.Runtime.InteropServices;
namespace MDINew
{
public class Class1
{
[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 flag, 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 void DoExitWin( int flag )
{
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( flag, 0 );
}
public void Reboot()
{
DoExitWin( EWX_REBOOT );
}
}
}
这个我的自动关机的几个类,非常容易调用:
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Security.Principal;
namespace closeauto
{
[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 Win32API
{
[DllImport("Advapi32", 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 PowerFlag
{
/// <summary>
/// 注销当前用户
/// </summary>
public static readonly int EWX_LOGOFF=0;
/// <summary>
/// 关闭计算机
/// </summary>
public static readonly int EWX_SHUTDOWN=1;
/// <summary>
/// 重新启动计算机
/// </summary>
public static readonly int EWX_REBOOT=2;
/// <summary>
/// 强制终止所有的进程
/// </summary>
public static readonly int EWX_FORCE=4 ;
/// <summary>
/// 关闭电源
/// </summary>
public static readonly int EWX_POWEROFF = 8 ;
}
/// <summary>
/// PowerOff 的摘要说明。
/// </summary>
public class PowerOff
{
public PowerOff()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static bool ForceToClose = false ;
public static bool ReSet()
{
return ExitWindow(PowerFlag.EWX_REBOOT+PowerFlag.EWX_LOGOFF) ;
}
public static bool ShutDown()
{
return ExitWindow(PowerFlag.EWX_POWEROFF+PowerFlag.EWX_LOGOFF) ;
}
public static bool ReLogin()
{
return ExitWindow(PowerFlag.EWX_LOGOFF) ;
}
private static bool ExitWindow(int iFlag)
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
IntPtr token = identity.Token;
IntPtr luid = (IntPtr)0;
IntPtr previousState = (IntPtr)0;
IntPtr previousStateLength = (IntPtr)0;
LUID privilegeId = new LUID ();
Win32API.LookupPrivilegeValue ("", "SeShutdownPrivilege", ref privilegeId);
TOKEN_PRIVILEGES privileges = new TOKEN_PRIVILEGES();
privileges.PrivilegeCount = 1;
privileges.Luid = privilegeId;
privileges.Attributes = 2;
Win32API.AdjustTokenPrivileges(token,false, ref privileges, Marshal.SizeOf(privileges), previousState, previousStateLength);
if (Win32API.GetLastError() != 0) return false ;
if (ForceToClose)
iFlag = iFlag + PowerFlag.EWX_FORCE ;
if(Win32API.ExitWindowsEx(iFlag, 0)) return false ;
return true ;
}
}//end class
}//end namespace