The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: brixton on July 25, 2005, 09:09:25 PM

Title: Shutdown messages
Post by: brixton on July 25, 2005, 09:09:25 PM
What are the messages that are sent to an app at shutdown?  Also, how do you set out a simple procedure which can look for this message and save data before it is due to quit?

Thanks for the help!  :U
Title: Re: Shutdown messages
Post by: Mark Jones on July 25, 2005, 09:24:25 PM
By shutdown, do you mean the window being closed || destroyed, or windows itself shutting down? I think there are separate messages for each. Start with WM_CLOSE and WM_DESTROY for window-closure messages, and see MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/system_shutdown_reference.asp) about the shutdown messages. :bg
Title: Re: Shutdown messages
Post by: Darrel on July 26, 2005, 07:18:38 AM
brixton,

From Win32API
QuoteThe WM_QUERYENDSESSION message is sent when the user chooses to end the Windows session or when an application calls the ExitWindows function. If any application returns zero, the Windows session is not ended. Windows stops sending WM_QUERYENDSESSION messages as soon as one application returns zero.

After processing this message, Windows sends the WM_ENDSESSION message with the wParam parameter set to the results of the WM_QUERYENDSESSION message.

Hope this helps,

Darrel
Title: Re: Shutdown messages
Post by: hutch-- on July 26, 2005, 08:07:41 AM
Give this a blast.


    invoke SendMessage,hWin,WM_SYSCOMMAND,SC_CLOSE,NULL


The "hWin" is te main app window handle.
Title: Re: Shutdown messages
Post by: brixton on July 26, 2005, 08:10:51 AM
Yes I mean when the computer is shut down, and 'Shut Down' is selected from the start menu.  Thankyou for the help, although I'm not so sure what you are attempting there, hutch--  :eek :red
Title: Re: Shutdown messages
Post by: PBrennick on July 26, 2005, 09:11:58 AM
Brixton,
I suspect that you are looking for something similar to CTRL_SHUTDOWN_EVENT.

Paul
Title: Re: Shutdown messages
Post by: Artoo on July 27, 2005, 12:16:00 AM
Try searching for this message:  WM_QUERYENDSESSION
or WM_ENDSESSION

:toothy
Title: Re: Shutdown messages
Post by: czDrillard on July 27, 2005, 03:26:33 PM
   .ELSEIF uMsg==WM_ENDSESSION

    ;do some stuff, like save files etc.

                .
                .
                .

                .ENDIF

QuoteWM_ENDSESSION

The WM_ENDSESSION message is sent to an application after the system processes the results of the WM_QUERYENDSESSION message. The WM_ENDSESSION message informs the application whether the session is ending.

A window receives this message through its WindowProc function.



LRESULT CALLBACK WindowProc(
  HWND hwnd,       // handle to window
  WM_ENDSESSION,   // the message to send
  WPARAM wParam,   // end-session option
  LPARAM lParam    // logoff option
);
Parameters
wParam
If the session is being ended, this parameter is TRUE; otherwise, it is FALSE.
lParam
If this parameter includes ENDSESSION_LOGOFF, the user is logging off; otherwise, the user is shutting down the system. (Note that this parameter is a bit mask. To test for this value, use a bit-wise operation; do not test for equality.)
Windows 2000/XP:  If this parameter is zero, the system is shutting down.

Return Values
If an application processes this message, it should return zero.

Remarks
If the wParam parameter is TRUE, the session can end any time after all applications have returned from processing this message. Therefore, an application should perform all tasks required for termination before returning from this message.

The application need not call the DestroyWindow or PostQuitMessage function when the session is ending.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, Windows 95.
Server: Included in Windows .NET Server 2003, Windows 2000 Server, Windows NT Server.
Header: Declared in Winuser.h; include Windows.h.


See Also
System Shutdown Overview, System Shutdown Messages, DestroyWindow, PostQuitMessage, WM_QUERYENDSESSION

hope this is helpful, best regards,

czDrillard
Title: Re: Shutdown messages
Post by: Hakima on July 30, 2005, 08:09:02 PM
What a nice information!!!  :U

Thanks!


Best regards
Title: Re: Shutdown messages
Post by: PBrennick on July 31, 2005, 12:19:55 AM
Try ExitWindowsEX,
It is better as it forces the apps down instead of waiting if you choose the correct flag.

Quote
The ExitWindowsEx function either logs off, shuts down, or shuts down and restarts the system.

BOOL ExitWindowsEx(

    UINT uFlags,   // shutdown operation
    DWORD dwReserved    // reserved
   );   


Parameters

uFlags

Specifies the type of shutdown. This parameter must be some combination of the following values:

Value   Meaning
EWX_FORCE   Forces processes to terminate. When this flag is set, Windows does not send the messages WM_QUERYENDSESSION and WM_ENDSESSION to the applications currently running in the system. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.
EWX_LOGOFF   Shuts down all processes running in the security context of the process that called the ExitWindowsEx function. Then it logs the user off.
EWX_POWEROFF   Shuts down the system and turns off the power. The system must support the power-off feature.Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. Windows 95: Security privileges are not supported or required.
EWX_REBOOT   Shuts down the system and then restarts the system. Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. Windows 95: Security privileges are not supported or required.
EWX_SHUTDOWN   Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped. Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. Windows 95: Security privileges are not supported or required.


dwReserved

Reserved; this parameter is ignored.



Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The ExitWindowsEx function returns as soon as it has initiated the shutdown. The shutdown or logoff then proceeds asynchronously.
During a shutdown or log-off operation, applications that are shut down are allowed a specific amount of time to respond to the shutdown request. If the time expires, Windows displays a dialog box that allows the user to forcibly shut down the application, to retry the shutdown, or to cancel the shutdown request. If the EWX_FORCE value is specified, Windows always forces applications to close and does not display the dialog box.

The ExitWindowsEx function sends a separate notification message, CTRL_SHUTDOWN_EVENT or CTRL_LOGOFF_EVENT as the situation warrants, to console processes. A console process routes these messages to its HandlerRoutine functions, which are added and removed by calls to the SetConsoleCtrlHandler function. ExitWindowsEx sends these notification messages asynchronously; thus, an application cannot assume that the console notification messages have been handled when a call to ExitWindowsEx returns.

Windows NT: To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information about security privileges, see Privileges.
Windows 95: Security privileges are not supported or required.

Paul