Test Case 30: Verify the application is Restart Manager Aware (Req:3.1)

 

Question: Can you provide some code samples to handle Restart manager messages?

Answer: The Restart Manager queries GUI applications for shutdown by sending a WM_QUERYENDSESSION notification that has the lParam parameter set to ENDSESSION_CLOSEAPP (0x1). Applications should not shut down when they receive a WM_QUERYENDSESSION message because another application may not be ready to shut down. GUI applications should listen for the WM_QUERYENDSESSION message and return a value of TRUE if the application is prepared to shut down and restart. If no application returns a value of FALSE, the Restart Manager sends a WM_ENDSESSION message with the lParam parameter set to ENDSESSION_CLOSEAPP (0x1) and the wparam parameter set to TRUE. Applications should shut down only when they receive the WM_ENDSESSION message. The Restart Manager also sends a WM_CLOSE message for GUI applications that do not shut down on receiving WM_ENDSESSION. If any GUI application responds to a WM_QUERYENDSESSION message by returning a value of FALSE, the shutdown is canceled. However, if the shutdown is forced, the application is terminated regardless.

 

When a GUI application receives a WM_ENDSESSION message, the application should prepare itself to shut down within the specified timeout period. At a minimum, applications should prepare by saving any user data and state information that is needed after a restart. It is recommended that applications periodically save the user data and state. You can go through the MSDN resource Guidelines for Applications for a detailed discussion. The following code in C++ does the same.

 

    private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)

    {

        IntPtr retVal = IntPtr.Zero;

        switch (msg)

        {

            case 0x0011: //WM_QUERYENDSESSION:

                if (lParam.ToInt32() == 0x1) // ENDSESSION_CLOSEAPP

                {

                    // App is being shtudown for restart (possibly by Restart Manager).

                    // If you do not return within 5 seconds, the shutdown may be blocked

                }

                else

                {

                    // User is logging off;

                    // Return a value of 0 if you want to veto your app being shutdown,

                    // otherwise return 1

                }

 

                handled = true;

 

                break;

 

            case 0x0016: //WM_ENDSESSION:

                if (lParam.ToInt32() == 0x1) // ENDSESSION_CLOSEAPP

                {

                    // App is being shutdown for restart

                    // You have 30 seconds to return control

                }

 

                handled = true;

                break;

        }

 

        return (retVal);

    }

 

// You’ll need this for the HwndSource class

using System.Windows.Interop;

// Run this code at start-up, possibly during the Load event of your main window

HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);

source.AddHook(new System.Windows.Interop.HwndSourceHook(WndProc));

There is no direct .NET API I know to implement the same. However, you can do that by using interop and Marshaling. Restart Manager and Generic Method Compilation is a very good resource on implementing the same in managed code.

 

Other Resources:

Restart Manager in Vista for developers

Vista: Restart Manager