如何使用 Inno Setup 确定是否安装了特定的 Windows 更新包 (KB*.msu)?

How to determine whether a specific Windows Update package (KB*.msu) is installed using Inno Setup?

我想知道如何确定目标计算机中是否安装了特定的 Windows 更新包,例如 Windows 名称为 [=12= 的更新包].

是否存在用于检查的内置功能?如果不是,确定它所需的代码是什么?可能会弄乱注册表,或者可能是最干净的 and/or 安全方式?

伪代码:

[Setup]
...

[Files]
Source: {app}\*; DestDir: {app}; Check: IsPackageInstalled('KB2919355')

[Code]
function IsPackageInstalled(packageName): Boolean;
  begin
    ...
    Result := ...;
  end;
function IsKBInstalled(KB: string): Boolean;
var
  WbemLocator: Variant;
  WbemServices: Variant;
  WQLQuery: string;
  WbemObjectSet: Variant;
begin
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WbemServices := WbemLocator.ConnectServer('', 'root\CIMV2');

  WQLQuery := 'select * from Win32_QuickFixEngineering where HotFixID = ''' + KB + '''';

  WbemObjectSet := WbemServices.ExecQuery(WQLQuery);
  Result := (not VarIsNull(WbemObjectSet)) and (WbemObjectSet.Count > 0);
end;

像这样使用:

if IsKBInstalled('KB2919355') then
begin
  Log('KB2919355 is installed');
end
  else 
begin
  Log('KB2919355 is not installed');
end;

学分:

当我在 Windows 7 上测试我的安装程序时,

WbemScripting.SWbemLocator 对我不起作用。所以我采取了不同的方法并连接到 WUA(Windows 更新代理) :

function IsUpdateInstalled(KB: String): Boolean;
var
  UpdateSession: Variant;
  UpdateSearcher: Variant;
  SearchResult: Variant;
  I: Integer;
begin
  UpdateSession := CreateOleObject('Microsoft.Update.Session');
  UpdateSearcher := UpdateSession.CreateUpdateSearcher()
  SearchResult := UpdateSearcher.Search('IsInstalled=1')
  for I := 0 to SearchResult.Updates.Count - 1 do
  begin
    if SearchResult.Updates.Item(I).KBArticleIDs.Item(0) = KB then
    begin
      Result := true;
      Exit;
    end;
  end;
  Result := false;
end;

调用如下:

if IsUpdateInstalled('3020369') then
...