public static void LogOff(ShutdownReason reasonFlag) { int Result; Result = User32ApiNativeMethods.ExitWindowsEx(ExitWindows.LogOff, reasonFlag); if (Result == 0) { throw new NolmeGenericException(string.Format(CultureInfo.InvariantCulture, "LogOff Failed")); } }
public static void Shutdown(ShutdownReason reasonFlag) { int Result; bool BooleanResult; TokenPrivileges tp = new TokenPrivileges(); long LocallyUniqueIdentifier; IntPtr hproc = (IntPtr)ApiNativeMethods.GetCurrentProcess(); int htok = 0; BooleanResult = ApiNativeMethods.OpenProcessToken((int)hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok); if (BooleanResult == false) { throw new NolmeGenericException(string.Format(CultureInfo.InvariantCulture, "OpenProcessToken Failed")); } tp.Count = 1; tp.LocallyUniqueIdentifier = 0; tp.Attributes = SE_PRIVILEGE_ENABLED; LocallyUniqueIdentifier = 0; BooleanResult = ApiNativeMethods.LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref LocallyUniqueIdentifier); if (BooleanResult == false) { throw new NolmeGenericException(string.Format(CultureInfo.InvariantCulture, "LookupPrivilegeValue Failed")); } tp.LocallyUniqueIdentifier = LocallyUniqueIdentifier; BooleanResult = ApiNativeMethods.AdjustTokenPrivileges(htok, false, ref tp, 0, 0, 0); if (BooleanResult == false) { throw new NolmeGenericException(string.Format(CultureInfo.InvariantCulture, "AdjustTokenPrivileges Failed")); } Result = User32ApiNativeMethods.ExitWindowsEx(ExitWindows.PowerOff | ExitWindows.Shutdown, reasonFlag); if (Result == 0) { throw new NolmeGenericException(string.Format(CultureInfo.InvariantCulture, "Shutdown Failed")); } }