using System;
using SystemRuntimeInteropServices;
class shoutdown{
[StructLayout(LayoutKindSequential Pack=)]
internal struct TokPrivLuid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport(kerneldll ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();
[DllImport(advapidll ExactSpelling=true SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h int acc ref IntPtr phtok );
[DllImport(advapidll SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host string name ref long pluid );
[DllImport(advapidll ExactSpelling=true SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok bool disall
ref TokPrivLuid newst int len IntPtr prev IntPtr relen );
[DllImport(userdll ExactSpelling=true SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg int rea );
internal const int SE_PRIVILEGE_ENABLED = x;
internal const int TOKEN_QUERY = x;
internal const int TOKEN_ADJUST_PRIVILEGES = x;
internal const string SE_SHUTDOWN_NAME = SeShutdownPrivilege;
internal const int EWX_LOGOFF = x;
internal const int EWX_SHUTDOWN = x;
internal const int EWX_REBOOT = x;
internal const int EWX_FORCE = x;
internal const int EWX_POWEROFF = x;
internal const int EWX_FORCEIFHUNG = x;
private static void DoExitWin(int flg)
{
bool ok;
TokPrivLuid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtrZero;
ok = OpenProcessToken( hproc TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY ref htok );
tpCount = ;
tpLuid = ;
tpAttr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null SE_SHUTDOWN_NAME ref tpLuid );
ok = AdjustTokenPrivileges( htok false ref tp IntPtrZero IntPtrZero );
ok = ExitWindowsEx( flg );
}
public static void Main()
{
ConsoleWriteLine(正在关闭计算机……);
// 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF EWX_REBOOT等实现不同得功能
// 在XP下可以看到帮助信息以得到不同得参数
// SHUTDOWN /?
DoExitWin(EWX_SHUTDOWN);
}
}