Using System;
using System.Runtime.InteropServices;
class shoutdown{
[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 flg, 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 flg)
{
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 (FLG, 0);
}
public static void Main ()
{
Console.WriteLine ("Shutting down the computer ...");
//Modify Ewx_shutdown or Ewx_logoff, ewx_reboot and so on to achieve different functions.
//Under XP you can see the help information to get the different parameters
//SHUTDOWN/?
Doexitwin (Ewx_shutdown);
}
}