none
Detect what kind of shutdown???

    Question

  • Hi guys...I'm trying to do something difficult here (at least for me)....
    I want to make a program that runs in background, and which saves some information to a file before the computer is shut down (by the user).....
    The program aborts the shutdown, saves the information, and AFTER THAT SHUTDOWNS THE COMPUTER...actually I'm making this "trick" to prevent information loss trough shutdown....but here's the situation:

    I don't exactly now what KIND of shutdown the user has executed: Reboot, Shutdown or Logoff.....
    I have to know this, so that if the user Logs off, the program would save the info, and after that LOGS OFF the computer, but not SHUT DOWN THE COMPUTER....

    CAN ANYBODY TELL ME HOW COULD A WINDOWS FORM APPLICATION (C#) DETECT THE TYPE OF SHUTDOWN THAT HAS BEEN EXECUTED?



    Note: I'm using the WM_QUERYENDSESSION int to instantly detect the shutdown.....
    THANKS IN ADVANCE!!
    Saturday, August 02, 2008 2:47 PM

Answers

  • Hi Zozel,

    As far as I know, when Windows trying to log-off, reboot or shutdown the system, typically, it will call the ExitWindowsEx Function to do this.

    In my opinion, we can try to hook the ExitWindowsEx Win32 API to determine what kind of shutdowns the system trying to is.

    But in this scenario, I think use C++ to do the API hook will be more convenient, we can create a C++ DLL to do it, and use the C# application to P/Invoke this DLL.

    In addition to API hook programming, please refer to the following article in code project.

    API hooking revealed

     Regards,

    Xun

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by jack 321 Wednesday, August 06, 2008 9:57 AM
    • Unmarked as answer by jack 321 Wednesday, August 06, 2008 9:57 AM
    • Edited by jack 321 Wednesday, August 06, 2008 9:57 AM edit a word
    • Marked as answer by jack 321 Thursday, August 07, 2008 11:31 AM
    Wednesday, August 06, 2008 3:43 AM
  • It is impossible for you to tell the difference between a shutdown and a reboot using the normal Windows API, therefore it is also impossible for you to do so via C#.

    The help for WM_QUERYENDSESSION says, for the lparam parameter: "If this parameter is zero, the system is shutting down or restarting (it is not possible to determine which event is occurring)."

    So I'm afraid the answer is: You can't do what you want to without hooking the ExitWindowsEx() function as Xun Ye says.
    • Edited by Matthew Watson Wednesday, August 06, 2008 9:56 AM amended
    • Marked as answer by jack 321 Thursday, August 07, 2008 11:31 AM
    Wednesday, August 06, 2008 9:54 AM

All replies

  • I'd use the objects in Microsoft.Win32 class, most notably the SystemEvents class.
    David Morton - Consultant - Catapult Systems - Houston
    Saturday, August 02, 2008 4:05 PM
    Moderator
  • Please expand the affirmation...could you post an example code?...Thx
    Saturday, August 02, 2008 4:29 PM
  •   public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          this.FormClosing += Form1_FormClosing;
          SystemEvents.SessionEnding += SystemEvents_SessionEnding;
        }

        void SystemEvents_SessionEnding(object sender, Microsoft.Win32.SessionEndingEventArgs e) {
          switch (e.Reason) {
            case SessionEndReasons.Logoff: Console.WriteLine("Log-off"); break;
            case SessionEndReasons.SystemShutdown: Console.WriteLine("Shutdown"); break;
          }
          this.Close();
        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e) {
          // Static event, unregister to be sure
          SystemEvents.SessionEnding -= SystemEvents_SessionEnding;
        }
      }


    Hans Passant.
    Saturday, August 02, 2008 5:28 PM
    Moderator
  • It doesn't work...I use the WndProc function for detecting shutdown...has anyone got any idea?....AND FOR THE REBOOT TOO....
    Sunday, August 03, 2008 10:37 AM
  • Hi Zozel,

    As far as I know, when Windows trying to log-off, reboot or shutdown the system, typically, it will call the ExitWindowsEx Function to do this.

    In my opinion, we can try to hook the ExitWindowsEx Win32 API to determine what kind of shutdowns the system trying to is.

    But in this scenario, I think use C++ to do the API hook will be more convenient, we can create a C++ DLL to do it, and use the C# application to P/Invoke this DLL.

    In addition to API hook programming, please refer to the following article in code project.

    API hooking revealed

     Regards,

    Xun

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by jack 321 Wednesday, August 06, 2008 9:57 AM
    • Unmarked as answer by jack 321 Wednesday, August 06, 2008 9:57 AM
    • Edited by jack 321 Wednesday, August 06, 2008 9:57 AM edit a word
    • Marked as answer by jack 321 Thursday, August 07, 2008 11:31 AM
    Wednesday, August 06, 2008 3:43 AM
  • It is impossible for you to tell the difference between a shutdown and a reboot using the normal Windows API, therefore it is also impossible for you to do so via C#.

    The help for WM_QUERYENDSESSION says, for the lparam parameter: "If this parameter is zero, the system is shutting down or restarting (it is not possible to determine which event is occurring)."

    So I'm afraid the answer is: You can't do what you want to without hooking the ExitWindowsEx() function as Xun Ye says.
    • Edited by Matthew Watson Wednesday, August 06, 2008 9:56 AM amended
    • Marked as answer by jack 321 Thursday, August 07, 2008 11:31 AM
    Wednesday, August 06, 2008 9:54 AM