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

删除回忆录丶 提交于 2019-12-03 20:46:43
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;

Use like:

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

Credits:

WbemScripting.SWbemLocator wasn't working for me when I was testing my installer on Windows 7. So I took a different approach and connected to the 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;

Invocation would be as follows:

if IsUpdateInstalled('3020369') then
...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!