Search code examples
windowsinstallationinno-setuppascalscript

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


I wonder how to determine whether a specific Windows Update package is installed in the target machine, lets say for example the Windows Update package with name KB2919355.

Exists a built-in feature to check that? If not, what would be the required code to determine it? Maybe messing with registry, or maybe a cleanest and/or secure way?

Pseudo-Code:

[Setup]
...

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

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

Solution

  • 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: