Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inno Setup: How to automatically uninstall previous installed version?

I'm using Inno Setup to create an installer.

I want the installer to automatically uninstall the previous installed version, instead of overwriting it. How can I do that?

like image 886
Quan Mai Avatar asked Jan 04 '10 15:01

Quan Mai


People also ask

Who invented Inno?

Kevin Gundersen - Founder - Inno Supps | LinkedIn.

Is Inno Setup free?

Inno Setup is a free installer for Windows programs by Jordan Russell and Martijn Laan.

How do I make an inno file executable?

Go to Menu, Project, then Compile to compile and create the setup file. This will create a complete installer. Run the Setup and your application will be installed correctly. Innosetup offers an awesome alternative to create great looking Installers for free.


2 Answers

I have used the following. I'm not sure it's the simplest way to do it but it works.

This uses {#emit SetupSetting("AppId")} which relies on the Inno Setup Preprocessor. If you don't use that, cut-and-paste your App ID in directly.

[Code]  { ///////////////////////////////////////////////////////////////////// } function GetUninstallString(): String; 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     RegQueryStringValue(HKCU, sUnInstPath, 'UninstallString', sUnInstallString);   Result := sUnInstallString; end;   { ///////////////////////////////////////////////////////////////////// } function IsUpgrade(): Boolean; begin   Result := (GetUninstallString() <> ''); end;   { ///////////////////////////////////////////////////////////////////// } function UnInstallOldVersion(): Integer; var   sUnInstallString: String;   iResultCode: Integer; begin { Return Values: } { 1 - uninstall string is empty } { 2 - error executing the UnInstallString } { 3 - successfully executed the UnInstallString }    { default return value }   Result := 0;    { get the uninstall string of the old app }   sUnInstallString := GetUninstallString();   if sUnInstallString <> '' then begin     sUnInstallString := RemoveQuotes(sUnInstallString);     if Exec(sUnInstallString, '/SILENT /NORESTART /SUPPRESSMSGBOXES','', SW_HIDE, ewWaitUntilTerminated, iResultCode) then       Result := 3     else       Result := 2;   end else     Result := 1; end;  { ///////////////////////////////////////////////////////////////////// } procedure CurStepChanged(CurStep: TSetupStep); begin   if (CurStep=ssInstall) then   begin     if (IsUpgrade()) then     begin       UnInstallOldVersion();     end;   end; end; 

Alternatives

See also this blog post "Inno Setup Script Sample for Version Comparison" which goes one step further, and reads the version number of any previously installed version, and compares that version number with that of the current installation package.

like image 162
Craig McQueen Avatar answered Sep 24 '22 13:09

Craig McQueen


You should be able to read the uninstall string from the registry, given the AppId (i.e. the value you used for AppID in the [Setup]-section). It could be found under Software\Microsoft\Windows\CurrentVersion\Uninstall\{AppId}\ (could be either HKLM or HKCU, so best check both) where {AppId} should be substituted with the actual value you used. Look for the UninstallString or QuietUninstallString values and use the Exec function to run it from your InitializeSetup() event function.

like image 26
Oliver Giesen Avatar answered Sep 20 '22 13:09

Oliver Giesen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!