/// <param name="ewxCode"></param> /// <returns>1 true if success shutdown else 0</returns> public static bool Shutdown(EWX ewxCode) { switch (ewxCode) { case EWX.EWX_FORCE: case EWX.EWX_LOGOFF: case EWX.EWX_POWEROFF: case EWX.EWX_SHUTDOWN: { DllImportCaller.lib.ShutdownOS(8); return(true); } case EWX.EWX_REBOOT: { DllImportCaller.lib.ShutdownOS(2); return(true); } default: { return(false); } } //uint v = (uint)ewxCode; //this cant be inside ShutdownOS( ... ) below as "ShutdownOS((uint)ewxCode)". Fails then... !??!?! //return DllImportCaller.lib.ShutdownOS((uint)ewxCode) == 1; //DllImportCaller.lib.ShutdownOS(8); //return true; }
/// <summary> /// コンピューターを強制的にシャットダウン・再起動・ログオフ・スタンバイ・休止状態にします。 /// </summary> /// <param name="shutDownSwitch"></param> public static void ShutDown(ShutDownSwitch shutDownSwitch) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) { adjustToken(); } EWX ewx = EWX.FORCE; switch (shutDownSwitch) { case ShutDownSwitch.PowerOff: ewx |= EWX.POWEROFF; break; case ShutDownSwitch.Reboot: ewx |= EWX.REBOOT; break; case ShutDownSwitch.LogOff: ewx |= EWX.LOGOFF; break; case ShutDownSwitch.StandBy: case ShutDownSwitch.Hibernate: bool hibernate = (shutDownSwitch == ShutDownSwitch.Hibernate); PowrProf.SetSuspendState(hibernate, true, true); return; default: throw new ArgumentException(); } User32.ExitWindowsEx(ewx, 0); }
/// <summary> /// 退出系统 /// </summary> /// <param name="flg">标识</param> public static bool DoExitWin(EWX flg) { bool ok; TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES(); IntPtr hproc = Process.GetCurrentProcess().Handle; IntPtr htok = IntPtr.Zero; ok = WindowsAPI.OpenProcessToken(hproc, TOKEN.TOKEN_ADJUST_PRIVILEGES | TOKEN.TOKEN_QUERY, ref htok); tp.PrivilegeCount = 1; tp.Privileges.Luid.HighPart = 0; tp.Privileges.Luid.LowPart = 0; tp.Privileges.Attributes = ConstValues.SE_PRIVILEGE_ENABLED; ok = WindowsAPI.LookupPrivilegeValue(null, ConstValues.SE_SHUTDOWN_NAME, ref tp.Privileges.Luid); ok = WindowsAPI.AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); ok = WindowsAPI.ExitWindowsEx(flg, 0); return(ok); }
/// <param name="ewxCode"></param> /// <returns>1 true if success shutdown else 0</returns> public static bool Shutdown(EWX ewxCode) { switch (ewxCode) { case EWX.EWX_FORCE: case EWX.EWX_LOGOFF: case EWX.EWX_POWEROFF: case EWX.EWX_SHUTDOWN: { DllImportCaller.lib.ShutdownOS(8); return true; } case EWX.EWX_REBOOT: { DllImportCaller.lib.ShutdownOS(2); return true; } default: { return false; } } //uint v = (uint)ewxCode; //this cant be inside ShutdownOS( ... ) below as "ShutdownOS((uint)ewxCode)". Fails then... !??!?! //return DllImportCaller.lib.ShutdownOS((uint)ewxCode) == 1; //DllImportCaller.lib.ShutdownOS(8); //return true; }
public static extern bool ExitWindowsEx(EWX uFlags, int dwReason);
public static extern bool ExitWindowsEx(EWX flg, int rea);