1

I am trying to use this code to uninstall the previous version (optionally, if it is installed) and uninstall silent from control panel:

#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() <> '');
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;

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.

How to correctly use both codes simultaneously?

Nico Z
  • 997
  • 10
  • 33
  • For detecting same version, see particularly the [answer by @MohsenB](https://stackoverflow.com/a/20288678/850848) – Martin Prikryl Nov 01 '17 at 19:30
  • And you should not be asking two different questions in one post anyway. – Martin Prikryl Nov 01 '17 at 19:31
  • What "all this code"? The code in your question? Or are you referring to some code in the answers, I've pointed you to? – Martin Prikryl Nov 01 '17 at 19:47
  • We know that your code does not work. That's why I've pointed you to [Executing UninstallString in Inno Setup](https://stackoverflow.com/q/42222356/850848), which is exactly about your problem! So please do read it! – Martin Prikryl Nov 01 '17 at 19:49
  • What is it that you do not understand? Are you still missing what is wrong with your code? – Martin Prikryl Nov 01 '17 at 20:07
  • 1
    Let's focus on the uninstall problem ok? You have to replace the code after `sUnInstallString := GetUninstallString();` with the code from https://stackoverflow.com/a/42222938/850848 – Martin Prikryl Nov 02 '17 at 06:45

0 Answers0