Delphi:检测挂起的重启(例如来自 Windows 更新)

Delphi: Detect a pending reboot (such as from Windows Update)

使用 Delphi,有没有办法检查挂起的重启(例如来自 Windows 更新)?

在我的研究中,我看到了一种使用 C++ (here) 执行此操作的方法,但它使用了一个我无法在 Delphi.[=13 中找到或找到等效项的库=]

您链接到的 Raymond Chen 的解决方案可以很容易地转换为 Delphi,尽管 Delphi 中的机制名称和语法略有不同。

documentation for ISystemInformation 说:

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

一个例子:

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.

您可以检查是否存在以下两个注册表项:

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

或注册表值

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager

如果这些键/值中的任何一个存在,则重启挂起。请注意,在 64 位 Windows 安装中,您应该查询 64 位注册表。有关如何从 32 位程序执行此操作的信息,请参阅 How can a 32-bit program read the “real” 64-bit version of the registry。此外,我相信第一个密钥 ...\Component Based Servicing\RebootPending 仅存在于 Vista / Server 2008 及更高版本中。