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

时间:2016-04-01 13:56:24

标签: windows installer inno-setup pascalscript

我想知道如何确定目标计算机中是否安装了特定的Windows Update软件包,例如名称为 KB2919355 的Windows Update软件包。

是否存在内置功能来检查?如果没有,那么确定它所需的代码是什么?也许搞乱注册表,或者可能是最干净和/或最安全的方式?

伪码:

[Setup]
...

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

[Code]
function IsPackageInstalled(packageName): Boolean;
  begin
    ...
    Result := ...;
  end;

2 个答案:

答案 0 :(得分:6)

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;

现金:

答案 1 :(得分:0)

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

WbemScripting.SWbemLocator对我不起作用。所以我采用了不同的方法并连接到WUA(Windows Update Agent):

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
...