Search code examples
registryinno-setuppascalscript

Inno Setup RegKeyExists returns False although the key exists


Can someone tell me what is wrong with this code?

[Code]
const
  OldVersionRegKey = '\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#MyAppId}_is1';
 
var
  HasOldVersion: Boolean;
 
function GetKey : Integer;
begin
  if IsWin64 then
    Result := HKLM64
  else
    Result := HKLM32;
end;
 
procedure CheckOldVersion;
begin
  HasOldVersion := RegKeyExists(GetKey, OldVersionRegKey);
  // ...
end;

Because whatever I do the answer is False while the key does exist?

I have tried everything, the key is not in the false result register, the key is in the false result register, on different levels of the database, on the 32 bit database, on the 64 bit database, test on both depending on the architecture (like the current code), but nothing to do, the result and always the same.

Thank you in advance for your help.


Solution

  • There should be no slash at the beginning of the key path. Check the example in RegKeyExists documentation.

    const
      OldVersionRegKey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#MyAppId}_is1';
    

    And as you are not using 64-bit install mode, the root key is always HKLM32.

    HasOldVersion := RegKeyExists(HKLM32, OldVersionRegKey);
    

    Or, if you want the script to be compatible with 64-bit install mode, use Is64BitInstallMode, instead of IsWin64.