I have an installer that write this line in the Windows registry
[Registry]
Root: "HKCU"; Subkey: "SOFTWARE\\W117GAMER"; ValueType: string
With use of RegQueryStringValue function and CompareVersion function from Compare version strings in Inno Setup (your question), you can do:
#define MyAppVersion "2.6"
[Code]
function InitializeSetup(): Boolean;
var
InstalledVersion: string;
VersionDiff: Integer;
begin
Result := True;
if not RegQueryStringValue(
HKCU, 'Software\My Program', 'DSVersionL4D2', InstalledVersion) then
begin
Log('No installed version detected');
end
else
begin
Log(Format('Found installed version %s', [InstalledVersion]));
VersionDiff := CompareVersion(InstalledVersion, '{#MyAppVersion}');
if VersionDiff < 0 then
begin
MsgBox(
Format('You have an old version %s installed, will uninstall it.', [
InstalledVersion]),
mbInformation, MB_OK);
{ Uninstall old version here }
end
else
if VersionDiff = 0 then
begin
MsgBox(
'You have this version installed already, cancelling installation.',
mbInformation, MB_OK);
Result := False;
end
else
begin
MsgBox(
Format(
'You have newer version %s installed already, cancelling installation.', [
InstalledVersion]),
mbInformation, MB_OK);
Result := False;
end;
end;
end;
Just plug-in an uninstallation code from some of the answers you have linked in your question.
Though note that you do not need to write your own version registry value. There are DisplayVersion, VersionMajor and VersionMinor in the stnadard uninstall registry key.