How can I tell if Windows Update is waiting for the system to reboot?


Today's Little Program shows how to ask whether Windows Update is waiting for the system to reboot. You might want to check this in your installer, so that you don't try to install your program while the system is in a mixed state where an update is partially-installed.

Testing this from script is easy. MSDN contains sample code to do that. Let's do it from C++. Today's smart pointer class is (rolls dice) _com_ptr_t! Remember that Little Programs do little to no error checking.

#include <windows.h>
#include <comdef.h> // for _COM_SMARTPTR_TYPEDEF
#include <wuapi.h>  // for ISystemInformation
#include <stdio.h>  // for printf (horrors! mixing stdio and C++!)

_COM_SMARTPTR_TYPEDEF(ISystemInformation, __uuidof(ISystemInformation));

int __cdecl main(int argc, char** argv)
{
 CCoInitialize init;
 ISystemInformationPtr info;
 info.CreateInstance(CLSID_SystemInformation);

 VARIANT_BOOL rebootRequired;
 info->get_RebootRequired(↦rebootRequired);

 printf("Reboot required? %d\n", rebootRequired);
 return 0;
}

Remember that VARIANT_BOOL uses -1 to represent VARIANT_TRUE, so if a reboot is required, you will see -1. Personally, I would treat any nonzero value as logically true.

Comments (45)
  1. Joshua says:

    [any nonzero as logically true]

    What a pleasant use for the !! operator.

  2. Neil says:

    Hmmm, is this any different (in overall result) than checking HKLMSOFTWAREMicrosoftWindowsCurrentVersionWindowsUpdateAuto UpdateRebootRequired ?

  3. EduardoS says:

    Does the "↦" have any meaning or is just a copy & paste error?

  4. Brian_EE says:

    @Neil: I would suppose that the API method is officially supported while pinging a registry key is not.

  5. Medinoc says:

    This Boolean remark reminds me of the TAR format and its numerous variants. In the end, I couldn't find enough information on whether a single-byte Boolean field was guaranteed (or even supposed) to be 0/1 or '0'/'1' (and on which TAR variant), so I decided to treat both 0 and '0' as logically false, and anything else as logically true.

  6. VinDuv says:

    @Eduardo: It’s a typo in the HTML code: @amp; -> @map; (which renders as an arrow because it apparently means “maps to”)

  7. Martin Bonner says:

    @VinDuv:  Eek!  That means Raymond must be typing the HTML by hand.  I knew he was hardcore, but that is _seriously_ hardcore!

  8. Another John says:

    What happens if you want to determine if a remote computer requires a reboot? If I have a server farm I might want to work out which machines were in need of rebooting.

  9. @Another John: Some googling around shows that there doesn't seem to be a good supported method for this, but it looks like someone worked out an "unsupported" way via remote registry (gallery.technet.microsoft.com/.../Get-PendingReboot-Query-bdb79542).  It's PowerShell, so you could easily pass it a list of computers to inspect.

  10. David Totzke says:

    @Another John: Ideally you wouldn't let servers just update themselves whenever new updates were available.  You'd schedule and control that and reboot them right away to make sure they come back up etc... Ideally you'd be using something like Microsoft System Center with Configuration Manager (or suitable third-party equivalent) to manage your farm.

  11. Myria says:

    I just want to figure out how to run Group Policy on Windows 10 Home so I can move Windows Update to manual mode... >.<

  12. Nick says:

    > (rolls dice)

    I really hope you keep a die on your desk that you actually roll for these.

    [(Looks around nervously.) (whisper) The dice are loaded. -Raymond]
  13. Billy O'Neal says:

    @Joshua Please do the world a favor and use != 0 instead

  14. Anon says:

    @Myria

    You cannot, and there's no reason for anyone on Home to be suspending Windows Updates indefinitely.

  15. Harry Johnston says:

    @Myria: making the appropriate registry changes (and setting permissions on the keys to disallow further modification) would probably do the trick.  Unsupported, of course.

  16. Cesar says:

    @Anon: yes there is; for instance you are going to travel tomorrow and don't want any surprises, or you are in the woods and your connection is very slow and shared with ten other people. Suspending updates until you are in a "safe" situation (for instance, at a friend's house with a fast Internet connection) is a good idea.

    @Martin Bonner: we all typed HTML by hand back in the 90's. Don't you remember all the "made with Notepad" icons? It's not that hardcore, just old-fashioned.

  17. foo says:

    @Another John. I would guess CoCreateInstanceEx (ie: using DCOM). Although using DCOM to solve a problem can be kind of like using regex [now you have two problems] if firewalls are involved or the machines cross trust boundaries, etc.

  18. RS says:

    That's handy. I wanted to implement an "I'm leaving" button since a while.

    if (updates_pending()) do_shutdown(); else do_sleep();

    ↓ PS: background info ↓:

    You know how windows 7 shows a fancy shut down button before it installs those updates? And how—to give the user an additional kick in the balls—it just shows the usual sleep button after the updates are installed and the countdown is running?

    Here’s a list of things which force you to disable auto-updates on Windows 7 (and thus avoid the automatic reboots)

    – watching movies full-screen

    – playing games full-screen (especially multiplayer games, and those hard-core games where you can't pause or save)

    – running a heavy processing job overnight

    – generally, working. It's easy to hide that popup when you're clicking around.

    It's hard to explain just how aggravating it is to have your PC reboot all of a sudden.

    So Windows 10 addresses this by scheduling reboots overnight, but you can't disable updates. This has the following problems:

    – disk trashing while installing updates. A laptop with a 5400rpm harddrive will be pretty much unresponsive while those updates are installed. It's pretty bad on a desktop with a 7200rpm drive (AFAIK installing on a mechanical drive is still officially supported).

    – network trashing on slow connections or connections with limited data volume. If you regularly use 3G tethering on your laptop you'll very badly screwed in most countries.

    – running a heavy processing job overnight can still screw up.

    – not sure how this will work in my case. I'm one of those people who never reboots his computer, but who suspends it overnight. I don't want my PC to reboot right after switching it on in the morning. That's why I want my fancy button I mentioned above.

    It's a hard problem to solve. You have to reboot at some point to install kernel updates. But for many users there is not really a convenient time.

  19. Christian says:

    Funny. Some hours ago I realized that under Windows 10 supplying power to your notebook means that Windows restarts itself automatically _without even asking_ if there are pending updates in queue. I skip my flame and the question of "why in hell do you do that?" for now because it is not relevant to this post.

  20. Engywuck says:

    @RS: well, my Win7 computer wakues up from sleep at 3AM, does its updates, reboots, and goes to sleep again. Isn't that default? OTOH my Win8.1 tablet shows "updates were installed. This computer will reboot this day if you don't do it yourself" -- for a week, if you put it to sleep between uses.

  21. jerzyszczur says:

    "@RS: well, my Win7 computer wakues up from sleep at 3AM, does its updates, reboots, and goes to sleep again. Isn't that default?"

    I am afraid your computer is possessed.

  22. Boris says:

    @Engywuck: how do you prevent the 3AM update from being blocked by applications which ask for data to be saved? I usually restart during the day, and every time I have to save the data I really need, then instruct Windows to force shutdown on the rest (since I can't be bothered to manually resolve each situation).

  23. SimonRev says:

    @Boris -- speaking from Windows 8 experience you just lose the data.  It has bit me once or twice at home.

    Fortunately one good thing from the phone app experience that is slowly trickling to the desktop experience is auto save.  I see more and more software that will just pick up where you left off when you restart it, regardless of whether you saved your documents.  I suspect this bit of Windows 10 will further encourage that behavior.

  24. Yukkuri says:

    @RS: The word you were looking for is 'thrashing', not 'trashing'.

  25. HdS says:

    The annoying thing about W10 is that the updates are even trying to install if the pc sleeps due to hibernate. Than the PC starts, discoers that some programs are running and stays awake. Which costs money and startles our pets which make quite a raket in the middle of the night.

    Thank microsoft for making hibernate unusuable...

  26. David Totzke says:

    @everybody-wanting-to-disable-delay-windows-updates:

    YOU are the reason we can't have nice things.  I don't see a single valid reason above that isn't just some manufactured scenario.

    - In the woods on a tethered connection?  Seriously?  You do know that by default Windows won't download updates over a metered connection don't you?  

    - 5400 RPM Laptop?  Do those even exist anymore?

    - It updates at the wrong time, I'm awake and working at 3AM - so reschedule the time no problem...

    All of my current machines reboot in under a minute and even my several-years-old laptop comes back under 2 minutes.  Go for a soda or empty yourself of the last one while it reboots.  Not wanting to reboot is so 1995.  You didn't want to because it took so long but now? Get a grip people.

    Ok, I feel better now.

  27. cheong00 says:

    @David: [In the woods on a tethered connection?  Seriously?  You do know that by default Windows won't download updates over a metered connection don't you?]

    Windows don't know it's on metered connection unless you're using a 3G/4G enabled version. If you're connecting it over "3G/4G connection sharing device" that's common when you travel to Japan (it allows flexible per day/per bandwidth usage rates, so is better choice than fixed bandwidth usage prepaid cards), it wouldn't know the difference.

  28. Chris Barts says:

    I'm probably dense, but is there a technical reason you used printf in your Little Program, or do you just prefer format strings to iostreams?

  29. Drak says:

    @David Totzke

    Ok, so yesterday evening I needed to quickly print out a document from my PC before taking the bus to a friend's place. My PC decided, while I was exploring for the document, to reboot.

    It then started installing updates which took about 10 minutes for who knows what reason (my PC has only SSDs, and an Intel i7, so I assume it should be "fast").

    Net result: I missed the bus, had to take a later one.

    Was I happy about this automatic reboot? No. Would I have been happy if I could have suspended it, say, 15 minutes? Yes.

  30. dead1ne says:

    @David

    I leave my PC on 24/7 and almost never reboot. Right now I have:

    3 Terminal Windows

    2 Browser Windows with 10 and 7 tabs

    1 Text Editor with 14 tabs

    4 Misc Apps

    I am not even actively working on anything right now if I am working there is usually more.

    Of those only the text editor can restart and return to its current state. For the browser windows the tabs do come back but the page state is reset ie my 3 paused youtube vids lose their place and start playing. For me any automatic reboot is unacceptable simply because of the effort to restore my environment. I need to be able to choose exactly when I reboot to apply an update at a time that will cause the least disruption. While you could blame the programs that cannot gracefully handle reboots I should not be punished for the fact that the applications I need cannot currently do so.

  31. Boris says:

    dead1ne is dead on. For me it tends to be open Visual Studio windows, perhaps a couple of Notepad windows with log files I didn't change deliberately, a bunch of open Word documents I didn't change deliberately either, etc. The solution is a couple of manual saves for items I did want to change + Force Restart.

  32. ender says:

    @David:

    > - In the woods on a tethered connection?  Seriously?  You do know that by default Windows won't download updates over a metered connection don't you?  

    I've got a computer at my weekend that's primarily used by my family members to check their e-mail (not everybody has a laptop, and some aren't comfortable using a smartphone). Since the weekend is mostly in use during the summer, there's no point in getting a DSL connection (there isn't even a phoneline), so we simply use a pre-paid 3G dongle. Guess what the update options are.

    > - It updates at the wrong time, I'm awake and working at 3AM - so reschedule the time no problem...

    I lost several hours of work when I left a long-running task overnight, and Windows decided it would be great to reboot for updates (and I never saw the notification that the update reboot is scheduled - because that only appears for about 30 seconds, and was apparently shown while I was away from my computer; so much for letting me plan the reboot). Luckily a group policy change worked around this problem, but good luck doing this on a non-domain-joined computer.

    > All of my current machines reboot in under a minute and even my several-years-old laptop comes back under 2 minutes.  Go for a soda or empty yourself of the last one while it reboots.  Not wanting to reboot is so 1995.  You didn't want to because it took so long but now? Get a grip people.

    Windows boots in about 30 seconds at work. Getting all of the programs I use open again and to the state they were before the reboot takes about 10 minutes. This is an i5 with 12GB RAM and SSD, but some programs are simply slow (and good luck getting the vendors to fix that).

  33. John Doe says:

    @David Totzke, you're gaining a lot of hate on this.  Here's some more.

    I want the computer to do what I need to do, not exactly what it needs to do.  If it needs to do something that messes up with what I want to do, the decision should go through me.

    YOU are the reason why we'll all find it normal to be subjugated by machines.  And I mean all, even those in high-command that everyone believes is telling the machines what to do and not the other way around.

    Go restart you PC.

    So seriously:

    - Tethering is the easiest way to have internet anywhere nowadays: smartphones, 3G wifi routers, dongles that pretend to be regular ethernet network cards, etc.  Windows couldn't be more wrong about what is or is not a metered connection, unless we could tell it.

    - Not only do slow disks exist, lots of Windows 7 and Windows 8 computers are from an era where SSD was too expensive for most pockets.

    - Rescheduling is only an option if you're staring at the computer when it decides it must reboot in X minutes in case you don't reschedule.

    The solution are live updates, which are essentially impossible in most scenarios.  Perhaps when you can virtualize kernel-mode drivers, user-mode drivers, applications from libraries, applications from the running environment, etc, that becomes possible.

  34. 640k says:

    @Brian_EE: I would suppose that the API method is officially supported while pinging a registry key is not.

    Pinging registry doesn't require heavy weight COM.

  35. David Totzke says:

    Safe by Design, Safe by Default, Safe in Depth. I believe that was the new mantra when Microsoft did a full-stop security sweep back in the XP-SP2 (?) days.  It's what everyone, to within experimental error, was and has been screaming for.  Now you have it and you're complaining.

    On a domain joined machine you are at the mercy of Group Policy, subvert that at your peril.  I'm at a place right now where they give you a day or two of a window and if you don't apply the advertised updates it gets done automatically and a reboot is forced.  I'm OK with that.  I was warned.

    On a pro version of Windows not joined to a domain you have control.  On Windows 10: Settings -> Windows Update - Advanced options -> Choose how updates are installed.  Select "Notify to schedule restart". If you didn't change the settings to something that suits you that's on you not Microsoft.

    Is there no place to choose when/how updates are installed with Home version? I haven't used a "home version" in forever. Personally anyone running home version can't be trusted [tongue-in-cheek] so automatic forced reboots are fine with me.

    Finally, I, for one, have always supported equal rights for artificially intelligent entities and look forward to serving our new cybernetic overlords. :)

  36. cheong00 says:

    @David: Tell me, for average PC users, how could you justify buying an OS that doubles the price when all the function you need from a business version is just the way to turn off Windows Update, when you can buy Win7 Home that does it.

    It just mean the only option we have when asked about whether Win7/8 Home users should upgrade to Win10 must be "Hell, no. If you upgrade, pray everytime you need to shutdown you don't get new updates, and pray everytime you need to update it won't break your computer, all because you won't be able to do anything to it."

    Go figure.

    Note: This is the advice I'm given to anyone asking me this question up to now, and I don't plan to change it unless Microsoft changed the decision to allow Home edition to have these option again.

  37. ender says:

    @David:

    > On a pro version of Windows not joined to a domain you have control.  On Windows 10: Settings -> Windows Update - Advanced options -> Choose how updates are installed.  Select "Notify to schedule restart". If you didn't change the settings to something that suits you that's on you not Microsoft.

    I set that, and came the next morning with the computer having rebooted and lost about 12 hours of work. The notification only works if you happen to be using the computer the moment it appears - and it only appears for about 30 seconds. If you were on a break while it appeared, you won't get any further notice, unless you go to Settings -> Updates and check there. According to you, that's what I should be doing every time I return to the computer after being away for more than a few seconds.

  38. David Totzke says:

    >>I set that, and came the next morning with the computer having rebooted and lost about 12 hours of work

    Let's imagine that you are given the option to suspend rebooting indefinitely.  How much work do you lose if the power goes out?  I'm willing to bet you don't have a backup generator.  Most UPS only give you enough run time to allow a clean shut-down.  This long-running process you have created should be robust to interruption and able to restart where it left off.  Again, that's on you, not Microsoft.  

    >>According to you, that's what I should be doing every time I return to the computer after being away for more than a few seconds.

    If 12 hours worth of work was at risk, I would definitely check that, probably twice, before I left for the night.

    >>how could you justify buying an OS that doubles the price when all the function'

    Easily.  How much does not having that function cost you over time?

     if(additionalWindowsCost < costOfLostWorkEtc) {goodInvestment = true;}

    >>and pray everytime you need to update it won't break your computer

    If updates are needed, your computer is already broken.  It might be broken in a way that allows the Bad Guys to take everything you have. You're going to have to do it at some point.  You seem to be asking for a zero-risk maintenance cycle.  That does not and cannot exist.

  39. John Doe says:

    @David Totzke:

    > If updates are needed, your computer is already broken. (...) You seem to be asking for a zero-risk maintenance cycle.  That does not and cannot exist.

    We disagree on both arguments.

    We're asking for the operating system to hold until WE tell it's OK to install updates.  It's different from never installing updates, but hey, even that should be supported e.g. in the name of having a reliable historical physical device (not VM) to test something.

    Then, 12 hours of work are not necessarily document-wise.  It might have to do with e.g. debugging a distributed system with multiple solutions and having the stars aligned to replicate a bug.  Sometimes, you just can't recover easily from this, so although you might not take 12 hours setting up everything the next time, the now-gone 12 hours are otherwise wasted.  And good luck setting up the environment and everything just like it was, even if you documented every step throughout.

    As for the Home edition, it's just the most used edition.  I agree it should install updates by default (and I think it has been so since Vista), but it should not restart without explicit permission (new in Windows 10).  And it's not like Home edition users deserve less, for instance, many are computer science students or developers in a "domainless" company with a cheap laptop.

  40. Harry Johnston says:

    As I just discovered, even Windows 10 Pro will reboot without the user's permission, even when set to "notify and schedule restart".  I wonder if it even respects the group policy settings?  That could really be a deal-breaker at my workplace.  We can't have people randomly losing work like that.

    (I swear it also didn't notify me that updates were available until I looked at the control panel.  They've been out for a couple of weeks, right?  I should have gotten them before now.)

    > How much work do you lose if the power goes out?

    Not the same thing.  Power cuts occur rarely and are an unavoidable risk.  Using an operating system that *deliberately* loses your work once or twice a month is neither.

  41. Alex Cohn says:

    Maybe I am missing something, but anybody can disable Windows Update by crafting an /etc/hosts file. This may be cheaper than upgrading from Home edition to perform all-night calculation or work with 3G internet. Actually, there are quite a few more hosts yours truly would consider to set '0.0.0.0' when internet downloads are expensive.

    Disclaimer: it would be wise to restore the windowsupdate entries and reboot as soon as possible, because, you know, not installing some update may be even worse for you and your computer than having it reboot at wrong moment.

  42. Klimax says:

    @Alex:

    Stopping service should be easier and still work. (I know it will stop download)

  43. ender says:

    @David:

    > I'm willing to bet you don't have a backup generator.  Most UPS only give you enough run time to allow a clean shut-down.

    I've got an UPS, and while the building (to my knowledge) doesn't have a backup generator, the UPS hasn't actually logged a power cut in 3 years I've had it (with the exception of the yearly scheduled maintenance, which is always announced a month before, and which is the only time I shut down my machine). Oh, and the UPS lets my machine run for 45 minutes.

    > Again, that's on you, not Microsoft.  

    This is on Microsoft, not me - not only has the behaviour changed from previous Windows versions, but the settings also lie by making you think you can avoid unexpected reboots, when that only happens in very rare circumstances.

    @Harry Johnston:

    > I wonder if it even respects the group policy settings?

    Luckily, it does, but I only discovered I need that after it already rebooted on me.

Comments are closed.

Skip to main content