//return true to avoid base.WndProc //https://www.microsoftpressstore.com/articles/article.aspx?p=2201310&seqNum=3 public static bool ProcessShutdownMessage(IntPtr hWnd, ref Message m) { ParseAndLogShutdownParameters(ref m); if (AbortShutdownIfScheduled && m.Msg == (int)Win32_Shutdown.eMsg.WM_QUERYENDSESSION) { Win32_Shutdown.ShutdownBlockReasonCreate(hWnd, "Block Unexpected Shutdown!"); Utils.Log.WriteLineF("ProcessShutdownMessage: ShutdownBlockReasonCreate(hWnd = 0x{0:X8})", (uint)hWnd); DialogResult res = MessageBox.Show("Allow Windows Shutdown/Reboot/Log off?", Environment.UserName, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); if (res == DialogResult.Yes) { Utils.Log.WriteLineF("Allow Windows Shutdown/Reboot/Log off..."); Win32_Shutdown.ShutdownBlockReasonDestroy(hWnd); return(false); //Allow Windows to shutdown } return(AbortShutdownIfScheduled); //don't call base.WndProc if aborting } return(false); //Allow Windows to shutdown }
private static void ProcessShutdownIfDetected() { int error_code = (int)Win32_Shutdown.InitiateShutdown(null, "Testing for Shutdown", 1024 * 1024, Win32_Shutdown.ShutdownFlags.SHUTDOWN_RESTARTAPPS, Win32_Shutdown.ShutdownReasons.SHTDN_REASON_MAJOR_APPLICATION | Win32_Shutdown.ShutdownReasons.SHTDN_REASON_MINOR_INSTALLATION | Win32_Shutdown.ShutdownReasons.SHTDN_REASON_FLAG_PLANNED); if (error_code == 0) //succeded to schedule shutdown - abort and continue { AbortSystemShutdown(null); } else if (error_code == Win32_Shutdown.ERROR_SHUTDOWN_IS_SCHEDULED) //this is what I am expecting to abort { AbortSystemShutdown(null); Utils.Log.WriteLineF("Shutdown schedule was detected and aborted!!!"); } else { Win32Exception e = new Win32Exception(error_code); Utils.Log.WriteLineF("Error Detecting Shutdown: " + e.Message); } }
public static int AbortSystemShutdown(string lpMachineName = "") { return(Win32_Shutdown.AbortSystemShutdown(lpMachineName)); }