Search code examples
inno-setuppascalscript

How do I check if registry key exists and exit setup


I try to making setup file to patch previous program. The setup must be able to check if previous program is installed or not.

This is my unusable code

[Code]
function GetHKLM() : Integer;

begin
 if IsWin64 then
  begin
    Result := HKLM64;
  end
  else
  begin
    Result := HKEY_LOCAL_MACHINE;
  end;
end;

function InitializeSetup(): Boolean;
var
  V: string;
begin
  if RegKeyExists(GetHKLM(), 'SOFTWARE\ABC\Option\Settings')
  then
    MsgBox('please install ABC first!!',mbError,MB_OK);
end;

My condition is

  • must check windows 32- or 64-bits in for RegKeyExists
  • if previous program is not installed, show error message and exit setup program, else continue process.

How to modify the code?
Thank you in advance.

**Update for fix Wow6432Node problem. I try to modify my code

[Code]
function InitializeSetup: Boolean;
begin
  // allow the setup to continue initially
  Result := True;
  // if the registry key based on current OS bitness doesn't exist, then...
if IsWin64 then
begin
 if not RegKeyExists(HKLM, 'SOFTWARE\Wow6432Node\ABC\Option\Settings') then
  begin
   // return False to prevent installation to continue
   Result := False;
   // and display a message box
   MsgBox('please install ABC first!!', mbError, MB_OK);
  end

  else
   if not RegKeyExists(HKLM, 'SOFTWARE\ABC\Option\Settings' then
    begin
    // return False to prevent installation to continue
    Result := False;
    // and display a message box
    MsgBox('please install ABC first!!', mbError, MB_OK);
   end
  end;
end;

Solution

  • The updated code in the question is incorrect -- you should never ever use Wow6432Node anywhere other than when looking at paths in RegEdit.

    From the behaviour you have described, you are actually looking for a 32-bit application. In this case you can use the same code regardless of whether Windows is 32-bit or 64-bit; you were overthinking your original question.

    Here is the corrected code from your updated question:

    [Code]
    function InitializeSetup: Boolean;
    begin
      // allow the setup to continue initially
      Result := True;
      if not RegKeyExists(HKLM, 'SOFTWARE\ABC\Option\Settings') then
      begin
        // return False to prevent installation to continue
        Result := False;
        // and display a message box
        MsgBox('please install ABC first!!', mbError, MB_OK);
      end;
    end;