3

Using Delphi, is there a way to check for a pending reboot (such as from Windows Update)?

In my research, I saw a way to do this using C++ (here) , but it uses a library that I could not locate or find an equivalent in Delphi.

Andre72127
  • 219
  • 3
  • 11

2 Answers2

8

The solution from Raymond Chen, to which you linked, can easily be translated to Delphi, although the mechanisms in Delphi have slightly different names and syntax.

The documentation for ISystemInformation says:

You can create an instance of this interface by using the SystemInformation coclass. Use the Microsoft.Update.SystemInfo program identifier to create the object.

An example:

program CheckRebootRequired;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, Winapi.ActiveX, System.Win.ComObj, System.Variants;

procedure Main;
var
  SysInfo: OleVariant;
  RebootRequired: OleVariant;
begin
  SysInfo := CreateOleObject('Microsoft.Update.SystemInfo');
  if not VarIsNull(SysInfo) then
  begin
    RebootRequired := SysInfo.RebootRequired;
    Writeln('Reboot required = ', RebootRequired);
  end
  else
    Writeln('Could not get Update SystemInfo');
end;

begin
  CoInitialize(nil);
  try
    try
      Main;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  finally
    CoUninitialize;
  end;
  Readln;
end.
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
2

You can check for the existence of the following two registry keys:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired

or the registry value

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager

If any of these keys / values exist a reboot is pending. Please take note that on 64 bit Windows installations you should query the 64 bit registry. See How can a 32-bit program read the “real” 64-bit version of the registry for information on how to do this from a 32 bit program. Furthermore, I believe the first key ...\Component Based Servicing\RebootPending only exists in Vista / Server 2008 and later.

Olaf Hess
  • 1,453
  • 11
  • 18