September 21st, 2015

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(&map;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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.