使用Delphi,有没有办法检查挂起的重启(例如从Windows Update)?
在我的研究中,我看到了一种使用C ++(here)执行此操作的方法,但它使用了我无法找到的库或在Delphi中找到等效的库。
您链接的Raymond Chen的解决方案可以很容易地转换为Delphi,尽管Delphi中的机制的名称和语法略有不同。
documentation for ISystemInformation说:
您可以使用SystemInformation coclass创建此接口的实例。使用Microsoft.Update.SystemInfo程序标识符来创建对象。
一个例子:
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及更高版本中。