Vista!
Shutting down or rebooting Windows allowing existing applications to reopen
Windows Vista introduced the /g switch in shutdown.exe and was unchanged in Windows 7:
/g Shutdown and restart the computer. After the system is
rebooted, restart any registered applications.
I never noticed it until Windows 10 which began actively use it when applying system updates: then suddenly many of the previously running applications would reopen during startup.
Note that a nice overview of the various shutdown.exe help texts are at [Wayback] MS-DOS and Windows command line shutdown command. I put the Windows 7, 8.1 and 10 help in the below gist revisions.
The major differences:
- Windows 8.x: “Shutdown” became “Full shutdown” in the
/s and /g switches, added /hybrid and /o switches:
/r Full shutdown and restart the computer.
/g Full shutdown and restart the computer. After the system is
rebooted, restart any registered applications.
...
/hybrid Performs a shutdown of the computer and prepares it for fast startup.
Must be used with /s option.
...
/o Go to the advanced boot options menu and restart the computer.
Must be used with /r option.
- Windows 10: amended help for
/g switch with “Automatic Restart Sign-On” information and /a switch with /fw information, and added /sg and /fw switches:
/sg Shutdown the computer. On the next boot, if Automatic Restart Sign-On
is enabled, automatically sign in and lock last interactive user.
After sign in, restart any registered applications.
...
/g Full shutdown and restart the computer. After the system is rebooted,
if Automatic Restart Sign-On is enabled, automatically sign in and
lock last interactive user.
After sign in, restart any registered applications.
/a Abort a system shutdown.
This can only be used during the time-out period.
Combine with /fw to clear any pending boots to firmware.
...
/fw Combine with a shutdown option to cause the next boot to go to the
firmware user interface.
It appears that Windows Vista already understood the /s and g combination to perform a full shutdown, but remember the applications that started.
Preparing your applications for reopen after Windows shutdown or restart
There is the [Wayback] RegisterApplicationRestart function (winbase.h) – Win32 apps | Microsoft Docs that allows you to register your application to restart when Windows restarts. You can listen to events to save your application state right before Windows restarts.
Some links:
- [Wayback] How to Develop Restart Manager Aware Application: Test Case 30 – CodeProject
- [Wayback] Automatically restart program when crashing? – embarcadero.delphi.rtl
- [Wayback] c# – Restart a crashed program with RegisterApplicationRestart without user prompt – Stack Overflow
- [Wayback] windows – What is the difference between “shutdown /r” and “shutdown /g”? – Super User
The /g option will restart applications that are registered for restart with the RegisterApplicationRestart API.
The Windows Restart Manager (introduced in Windows Vista) supports gracefully shutting down and restarting applications that registered for restart with the RegisterApplicationRestart API.
This functionality is used by Windows Update – thanks to the Restart Manager, when I come yawning to my desktop PC in the morning, even following a system restart, I have my Outlook, browser windows, OneNote, Visual Studio, and Messenger all lined up as they were when I went to bed.
Suppose you want to initiate one of these “automagically restart everything after restart” restarts. As of a few weeks ago, I had it in my head that you have to write a small app that uses the Restart Manager APIs (e.g. RmStartSession and RmShutdown) to do this.
And then it hit me that the shutdown command must have support for doing this. And indeed, it has:
shutdown /g
Source Restart Windows and Restart All Registered Applications: shutdown -g
- [Wayback] How come Word is back after Setup.exe restarts my PC? at Fabian’s Mix
Vista (and thus Server 2008) contains a new Restart Manager, which enables this feature and is used automatically by Windows Installer 4.
To enable your application to restore its sessions, you simply listen for WM_QUERYENDSESSION with an lParam of ENDSESSION_CLOSEAPP (save your state there) and register your application for restart using RegisterApplicationRestart (which is part of the automatic recovery functions, see the MSDN for the P/invoke signatures). If you have a console application, you can also use a CTRL_C_EVENT handler (or the Console.CancelKeyPress event) to save your state, but in this case, you have to call RegisterApplicationRestart before you get that notification. See the MDSN for more comprehensive guidelines.
And that’s it, your application is ready for a clean Windows Update experience.
- [Wayback] The Moth – Vista: Restart Manager
…
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_QUERYENDSESSION) //0x0011
{
if ((m.LParam.ToInt32() & ENDSESSION_CLOSEAPP) == ENDSESSION_CLOSEAPP) //0x1
{
// some installation will shut us down next, so do some cleanup (prepare for it)
File.WriteAllText(Environment.CurrentDirectory + @"\rm.txt", "for demo purposes");
//MessageBox.Show("intercepted", “RM request”);
}
}
}
…
Jen Gentleman on shutdown.exe switches
Some posts by Jen Gentleman and replies to them via [Archive.is] @JenMsft shutdown – Twitter Search:
All of the above because of [Wayback] Thread by @jpluimers on Thread Reader App – Thread Reader App:
I remembered that, when I was in hospital, @JenMsft a while back mentioning that you can manually shutdown and save apps for reopen after booting.
She did, multiple times: [Archive.is] twitter.com/search?q=from%…
It’s the /g switch of shutdown.exe
I wondered how long ago that was introduced…
I still have Windows XP and Windows 7 VM’s for experiments like this. Windows XP shutdown.exe didn’t have it, but Windows 7 shutdown.exe had.
So when was it introduced?
… drumroll …
superuser.com/questions/1132091…
Windows Vista!
I’ve been living under a stone for a long time (:
The superuser post also mentions docs.microsoft.com/en-gb/windows/…RegisterApplicationRestart [Wayback]
I sense a blog post coming up about it.
Thanks @JenMsft for teaching an old fart something new!
/t 1 versus /t 0 and Windows XP shutdown.exe help
When writing this, I wondered why some people consistently use /t 1 (timeout ) and others /t 0, and even asked for explanation [Archive.is] Jeroen Wiert Pluimers on Twitter: “why /t 1 instead of /t 0 ?… “ (but no answer).
The reason is simple:
/t xxx Set the time-out period before shutdown to xxx seconds.
The valid range is 0-315360000 (10 years), with a default of 30.
If the timeout period is greater than 0, the /f parameter is
implied.
...
/f Force running applications to close without forewarning users.
The /f parameter is implied when a value greater than 0 is
specified for the /t parameter.
/t 0 does not imply /f
/t 1 implies /f, but the time-out is so short that nobody can cancel, so it is sort of immediate
Looking at the Windows XP help for shutdown.exe, I realised two things:
- I never looked at the switches after Windows XP, so I totally missed the
/f, /g and /sg ones
- People still use the minus sign (
-) to prefix shutdown.exe switches instead of the slash (/) as that was the preferred prefix until Windows XP.
Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "comment"] [-d up:xx:yy]
No args Display this message (same as -?)
-i Display GUI interface, must be the first option
-l Log off (cannot be used with -m option)
-s Shutdown the computer
-r Shutdown and restart the computer
-a Abort a system shutdown
-m \\computername Remote computer to shutdown/restart/abort
-t xx Set timeout for shutdown to xx seconds
-c "comment" Shutdown comment (maximum of 127 characters)
-f Forces running applications to close without warning
-d [u][p]:xx:yy The reason code for the shutdown
u is the user code
p is a planned shutdown code
xx is the major reason code (positive integer less than 256)
yy is the minor reason code (positive integer less than 65536)
–jeroen
Gist with Windows 7, 8.1 and 10 help for shutdown.exe.
https://gist.github.com/jpluimers/a3d7584b88ab5f33c9de51c161fdfdc3
Related
Leave a Comment