title: Inno Setup - Uninstall the previous version correctly
body: <p>I am trying to use this code to uninstall the previous version (optionally, if it is installed) and uninstall silent from control panel:</p>
  
  <pre><code>#define AppId "Program158"
  
  [code]
  
  procedure ChangeUninstallString;
  var
    sUnInstPath: String;
    sUnInstallString: String;
  begin
    sUnInstPath := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting("AppId")}_is1');
    sUnInstallString := '';
    if not RegQueryStringValue(HKLM, sUnInstPath, 'UninstallString', sUnInstallString) then begin
      RegQueryStringValue(HKCU, sUnInstPath, 'UninstallString', sUnInstallString);
      RegWriteStringValue(HKCU, sUnInstPath, 'UninstallString', sUnInstallString + '/SILENT');
    end
    else begin
      RegWriteStringValue(HKLM, sUnInstPath, 'UninstallString', sUnInstallString + '/SILENT');
    end;
  end;
  
  procedure CurStepChanged(CurStep: TSetupStep);
  begin
    if CurStep=ssPostInstall then begin
     ChangeUninstallString;
    end;
  end;
  
  function GetUninstallString: string;
  var
    sUnInstPath: string;
    sUnInstallString: String;
  begin
    Result := '';
    sUnInstPath := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\Program156_is1'); { Your App GUID/ID }
    sUnInstallString := '';
    if not RegQueryStringValue(HKLM, sUnInstPath, 'UninstallString', sUnInstallString) then
      RegQueryStringValue(HKCU, sUnInstPath, 'UninstallString', sUnInstallString);
    Result := sUnInstallString;
  end;
  
  function IsUpgrade: Boolean;
  begin
    Result := (GetUninstallString() &lt;&gt; '');
  end;
  
  function InitializeSetup(): Boolean;
  var
    V: Integer;
    iResultCode: Integer;
    sUnInstallString: string;
  begin
    Result := True; { in case when no previous version is found }
    if RegValueExists(HKEY_LOCAL_MACHINE,'Software\Microsoft\Windows\CurrentVersion\Uninstall\Program156_is1', 'UninstallString') then  { Your App GUID/ID }
    begin
      V := MsgBox(ExpandConstant('Hey! An old version of app was detected. Do you want to uninstall it?'), mbInformation, MB_YESNO); { Custom Message if App installed }
      if V = IDYES then
      begin
        sUnInstallString := GetUninstallString();
        sUnInstallString :=  RemoveQuotes(sUnInstallString);
        Exec(ExpandConstant(sUnInstallString), '', '', SW_SHOW, ewWaitUntilTerminated, iResultCode);
        Result := True; { if you want to proceed after uninstall }
        { Exit; //if you want to quit after uninstall }
      end
      else
        Result := False; { when older version present and not uninstalled }
    end;
  end;
  </code></pre>
  
  <p>This code does not work correctly if both ideas are used simultaneously. It only works well if I use the code to detect the previous version and the uninstaller runs. If I use both ideas simultaneously (all this code), if I choose to uninstall the previous version of my program, the uninstaller does not run.</p>
  
  <p>How to correctly use both codes simultaneously?</p>