How to simulate Windows shutdown for debugging?
I believe when Windows is shutting down it sends a "WM_QueryEndSession" to all applications. To simulate a Windows shutdown you could create a little application that just does a PostMessage with this message to your application and see what happens. Windows may send more messages than that to actually close your application (like WM_CLOSE), but whenever your application receives the "WM_QueryEndSession" message it means your application is about to have the rug pulled out from under it.
This can be done using rmlogotest.exe
tool. See following link:
https://superuser.com/questions/959364/on-windows-how-can-i-gracefully-ask-a-running-program-to-terminate#1154058
If you're using WPF, then you can handle additional event:
Application.SessionEnding += Application_SessionEnding;
where you can set e.Cancel = true;
to be able to abort application closure.
I think rmlogotest.exe
uses timeout of 5 seconds and then it will tell you that
LOGO Validation FAILED
.
If you put some sort of message box confirmation in your application - you get failure as for end-user it will take probably more than 5 seconds to respond.
I guess from windows perspective if 5 seconds passed by, it want to reboot anyway - and if end-user hasn't saved his document / work - then windows is expecting to copy it somewhere else maybe.
From my perspective (as application developer) - it's acceptable to have failing
LOGO Validation FAILED
, as user can decide on it's own - whether he will kill my application after 5 seconds and looses his document or he will save the document and closes my application manually.
If you need to broadcast WM_QUERYENDSESSION
message, then it needs to be sent via
SendMessage
to all windows in current process. Starting sample code can be found from here:
https://github.com/qakit/headless.git
But it sends only WM_ENDSESSION
message, you need to use WM_QUERYENDSESSION
instead and add windows enumeration of given process.
Related links:
- https://devblogs.microsoft.com/oldnewthing/20130627-00/?p=3973
- https://devblogs.microsoft.com/oldnewthing/20170329-00/?p=95855
There is a tool named Restart Manager (rmtool.exe) in the Microsoft's Logo Testing Tools for Windows, which can be used to send shutdown and restart messages to a process. Logo testing tools can be downloaded here:
http://download.microsoft.com/download/d/2/5/d2522ce4-a441-459d-8302-be8f3321823c/LogoToolsv1.0.msi
Then you can simulate shutdown for your process:
rmtool.exe -p [PID] -S
where [PID] is the process ID. According to the Vista Logo Certification Test Cases document,
Restart Manager shutdown messages are:
a. WM_QUERYENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1): GUI applications must respond (TRUE) immediately to prepare for a restart.
b. WM_ENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1): The application must shutdown within 5 seconds (20 seconds for services).
c. CTRL_SHUTDOWN_EVENT: Console applications must shutdown immediately.
SendMessage can be used to send window messages with any parameters to a window.
Very useful for debugging and testing.
Send the message
WM_QUERYENDSESSION
withLPARAM
=ENDSESSION_CLOSEAPP
. The application must return 1 (TRUE) to indicate it's prepared to shut down and restart.Send the message
WM_ENDSESSION
with LPARAM =ENDSESSION_CLOSEAPP
The application must shut down within the specified timeout period.