registryinstallationinno-setup

Detect and uninstall old version of application in Inno Setup using its version number stored in registry


I have an installer that write this line in the Windows registry

[Registry]
Root: "HKCU"; Subkey: "SOFTWARE\W117GAMER"; ValueType: string; ValueName: "DSVersionL4D2"; ValueData: "{#MyAppVersion}"

taking into account that {#MyAppVersion} is defined and written when the program is installed

#define MyAppVersion "2.7"

I am constantly updating the installer, which is why some people have old installations, and when they update, old files that conflict are combined, so as not to uninstall the previous version, there is some way to read this registry before starting the installation.

I read previous posts but they only work with "GUID" or "appID" of the program, try to modify some lines of code but I could not get anything, if someone could help me I thank you in advance, sorry for my English I use a translator I am from Latin America

How to detect old installation and offer removal?

Inno Setup: How to automatically uninstall previous installed version?


Solution

  • 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.