8

I have just started using inno setup, and it seems to work well. However, when I run the installer with the app already installed it reinstalls. I would like to give the user to uninstall. Is this possible, and if so, how can it be done?

To be specific, I have written a game for a homework assignment. I made an installer using inno setup. The app installs fine and can be uninstalled using the control panel, but my professor would like to be able to uninstall the application by re-running the installer and choosing an uninstall option. This will save him time since he has about 50 of these assignments to mark.

Thanks,

Gerry

gmoerkerken
  • 149
  • 1
  • 1
  • 8
  • It's quite hard to guess what you're going to do. Do you want for instance ask users if they want to uninstall the application when it's already installed ? I mean, when the user starts the installer, a confirmation message like *Application is already installed, do you want to uninstall it first ?* would popup. – TLama Jan 21 '13 at 20:38
  • This is exactly what I am trying to do. If the application is already installed, I don't want to overwrite this installation without giving the user the option to "repair" or "uninstall" – gmoerkerken Jan 21 '13 at 20:49
  • So to be more specific, you want to offer your user a dialog *Application is already installed, do you want to uninstall it first ?* when the installer starts and when the user choose *Yes*, the previous application will be uninstalled, right ? – TLama Jan 21 '13 at 20:55
  • I've updated the question to be more specific. – gmoerkerken Jan 21 '13 at 21:00
  • I think you can find your answer here : http://wix.sourceforge.net/manual-wix3/create_an_uninstall_shortcut.htm – Zz Oussama Jan 21 '13 at 20:41

3 Answers3

13

The next script will make the following options form when the application is already installed on the target system when the setup is started:

enter image description here

When the user clicks Repair button, the setup is normally started. When user clicks the Uninstall button, the previously installed application is uninstalled. When user closes that form, nothing happens.

Here is the script (don't forget to specify, ideally some unique, value for the AppId setup directive in your script):

[Setup]
AppName=My Program
AppVersion=1.5
AppId=1C9FAC66-219F-445B-8863-20DEAF8BB5CC
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output

[CustomMessages]
OptionsFormCaption=Setup options...
RepairButtonCaption=Repair
UninstallButtonCaption=Uninstall

[Code]
const
  mrRepair = 100;
  mrUninstall = 101;

function ShowOptionsForm: TModalResult;
var
  OptionsForm: TSetupForm;
  RepairButton: TNewButton;
  UninstallButton: TNewButton;
begin
  Result := mrNone;
  OptionsForm := CreateCustomForm;
  try
    OptionsForm.Width := 220;
    OptionsForm.Caption := ExpandConstant('{cm:OptionsFormCaption}');
    OptionsForm.Position := poScreenCenter;

    RepairButton := TNewButton.Create(OptionsForm);
    RepairButton.Parent := OptionsForm;
    RepairButton.Left := 8;
    RepairButton.Top := 8;
    RepairButton.Width := OptionsForm.ClientWidth - 16;
    RepairButton.Caption := ExpandConstant('{cm:RepairButtonCaption}');
    RepairButton.ModalResult := mrRepair;

    UninstallButton := TNewButton.Create(OptionsForm);
    UninstallButton.Parent := OptionsForm;
    UninstallButton.Left := 8;
    UninstallButton.Top := RepairButton.Top + RepairButton.Height + 8;
    UninstallButton.Width := OptionsForm.ClientWidth - 16;
    UninstallButton.Caption := ExpandConstant('{cm:UninstallButtonCaption}');
    UninstallButton.ModalResult := mrUninstall;

    OptionsForm.ClientHeight := RepairButton.Height + UninstallButton.Height + 24;
    Result := OptionsForm.ShowModal;
  finally
    OptionsForm.Free;
  end;
end;

function GetUninstallerPath: string;
var
  RegKey: string;
begin
  Result := '';
  RegKey := Format('%s\%s_is1', ['Software\Microsoft\Windows\CurrentVersion\Uninstall', 
    '{#emit SetupSetting("AppId")}']);
  if not RegQueryStringValue(HKEY_LOCAL_MACHINE, RegKey, 'UninstallString', Result) then
    RegQueryStringValue(HKEY_CURRENT_USER, RegKey, 'UninstallString', Result);
end;

function InitializeSetup: Boolean;
var
  UninstPath: string;
  ResultCode: Integer;  
begin
  Result := True;
  UninstPath := RemoveQuotes(GetUninstallerPath);
  if UninstPath <> '' then
  begin
    case ShowOptionsForm of
      mrRepair: Result := True;
      mrUninstall: 
      begin
        Result := False;
        if not Exec(UninstPath, '', '', SW_SHOW, ewNoWait, ResultCode) then
          MsgBox(FmtMessage(SetupMessage(msgUninstallOpenError), [UninstPath]), mbError, MB_OK);
      end;
    else
      Result := False;
    end;
  end;
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
3

For some reason your code

RegKey := Format('%s\%s_is1', ['Software\Microsoft\Windows\CurrentVersion\Uninstall', 
    '{#emit SetupSetting("AppId")}']);

returned an extra { to the _is1 value. I didn't had the time to check why or where i was wrong in my implementation, all i confirm is that my installer works with the

RegKey := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting("AppId")}_is1');

alternate.

Hope it helps.

Thank you for the code sample.

1

When using Inno Setup, there's no reason to uninstall a previous version unless that version was installed by a different installer program. Otherwise upgrades are handled automatically.

Your answer is here :

InnoSetup: How to automatically uninstall previous installed version? previous-installed-version

Community
  • 1
  • 1
Zz Oussama
  • 229
  • 1
  • 12