For SoundSwitch, I needed to fix a bug where the installer wasn’t detecting correctly the version of VC Redist C++ installed.

As I found on Stack Overflow, each version has it’s own MSI key in the registry, but this is impossible to use since at each update of the VC Redist you need to find the new registry key and check for it.

However, I found this excellent post explaining where in the registry you can find if the wanted version is installed. The best part, the build is also present at the key given.

With that information at hand, I had all I needed to future proof my installer, and I’m happy to share with you the script I wrote for Inno Setup.

[Code]
function IsX64: boolean;
begin
	Result := Is64BitInstallMode and (ProcessorArchitecture = paX64);
end;

procedure Explode(var Dest: TArrayOfString; Text: String; Separator: String);
var
  i, p: Integer;
begin
  i := 0;
  repeat
    SetArrayLength(Dest, i+1);
    p := Pos(Separator,Text);
    if p > 0 then begin
      Dest[i] := Copy(Text, 1, p-1);
      Text := Copy(Text, p + Length(Separator), Length(Text));
      i := i + 1;
    end else begin
      Dest[i] := Text;
      Text := '';
    end;
  until Length(Text)=0;
end;

function IsVcRedistInstalled(version: String; build: cardinal): boolean;
var
  regBuildNumber: cardinal;
  regKey: String;
  regInstalled: cardinal;
  regStringBuildNumber: String;
  strArrayBuildNumber: TArrayOfString;
begin
  Log('Check for VC Redist ' + version + ' with build ' + IntToStr(build));
  if(IsX64()) then begin
    regKey := 'SOFTWARE\WOW6432Node\Microsoft\VisualStudio\'+ version +'\VC\Runtimes\x64';
  end else begin
    regKey := 'SOFTWARE\Microsoft\VisualStudio\'+ version +'\VC\Runtimes\x86';
  end;
  RegQueryDWordValue(HKLM, regKey, 'Installed', regInstalled);
   //Normal version installed, compare buildNumber
  if(regInstalled = 1) then begin
    Log('VC Redist ' + version + ' Installed');
    RegQueryDWordValue(HKLM, regKey, 'Bld', regBuildNumber);
    Result:= (regBuildNumber >= build);
    Log('Detected VC Redist Build ' + IntToStr(regBuildNumber));
    Exit;
  end;
    Log('VC Redist ' + version + ' not in default directory registry.')

  //Check developer version
  if(IsX64()) then begin
    regKey := 'SOFTWARE\Wow6432Node\Microsoft\DevDiv\vc\Servicing\'+ version +'\RuntimeAdditional';
  end else begin
    regKey := 'SOFTWARE\Microsoft\DevDiv\vc\Servicing\'+ version +'\VC\RuntimeAdditional\x86';
  end;
  RegQueryDWordValue(HKLM, regKey, 'Install', regInstalled);
  if(regInstalled <> 1) then begin
    Result := false;
    Exit;
  end;

  RegQueryStringValue(HKLM, regKey, 'Version', regStringBuildNumber);
  Explode(strArrayBuildNumber, regStringBuildNumber, '.');
  regBuildNumber := StrToInt(strArrayBuildNumber[2]);
  Result:= (regBuildNumber >= build);  
end;