public static void Shutdown(bool force)
{
if (force)
{
DoExitWin(EWX_SHUTDOWN | EWX_FORCE);
}
else
{
DoExitWin(EWX_SHUTDOWN | EWX_FORCEIFHUNG);
}
}
public static void DoExitWin(int flg)
{
//give current process SeShutdownPrivilege
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
if (!OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok))
{
throw new Exception("Open Process Token fail");
}
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
if (!LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid))
{
throw new Exception("Lookup Privilege Value fail");
}
if (!AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero))
{
throw new Exception("Adjust Token Privileges fail");
}
//Exit windows
if (!ExitWindowsEx(flg, 0))
{
throw new Exception("Exit Windows fail");
}
}