HOWTO: uninstall a program from Inno Setup kit

Introduction

One of the many challenges in releasing the latest version of Horodruin was that the main edition switched to 64-bits.

This means that we cannot simply install/update the new program over the existing one, mainly because 64 bits programs have a different default system folder (‘Program Files‘) than 32 bits ones (‘Program Files (x86)‘).

The best option here is to uninstall the old one, before installing the new package.

The solution below will detect the outdated program installation and will uninstall it.

The solution

All the needed info is usually stored under the following registry key:

SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\

But, in my case, I need to seek a 32 bits installation on 64 bits Windows that are stored under this other registry key:

SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\

In these keys are present the installation data per program’s unique name.
So our next step is to determine this unique name.  For Inno Setup assisted program, it should be “<program_name>_is1“.

When we have all the needed elements we can write down a small piece of code in the [Code] section in our Inno Setup script.

{ remove previous 32-bits installation }
function Uninstall32bitsVersion () : Boolean;
var
  sUninstall: string;
  ResultCode: Integer;
begin
  Result := True;
  { is it installed? }
  if RegQueryStringValue (HKLM,
      'SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\MyOld32bitProgram_is1',
      'UninstallString', sUninstall) then
  begin
    { notify & confirm the operation }
    if (MsgBox ('Warning: the current 32-bits version of <my_old_program> will be uninstalled!',
        mbInformation, MB_OKCANCEL) <> IDOK) then
    begin
      Result := False;
    end
    else
      { uninstall it }
      ShellExec ('', sUninstall, '/SILENT', '', SW_SHOWNORMAL,
          ewWaitUntilTerminated, ResultCode);
  end;
end;

{ installation process interface }
function InitializeSetup(): Boolean;
begin
  Result := Uninstall32bitsVersion ();
end;

Using this piece of code above, your setup will:

  • detect old 32 bits program instance, and if it is present:
  • it will ask to uninstall it, giving the chance to abort the operation.
  • else it will uninstall it before the new package processing starts.