How do I require user to uninstall previous version with NSIS
Asked Answered
L

3

12

I have just started using NSIS.
It works very well but I find the documentation a bit unstructured. How do I require user to uninstall previous version before installing a new version with NSIS?

NSIS (Nullsoft Scriptable Install System) is an open source system to create Windows installers.

Laryngology answered 5/4, 2009 at 20:31 Comment(0)
K
14

NSIS is a great Windows Installer. Here is how I use NSIS to uninstall the current version while installing a new version of the same application. Add the following function to your NSIS script.

Function .onInit

         Exec $INSTDIR\uninst.exe 

FunctionEnd

Also you can check out this link on the NSIS wiki on "Auto-uninstall old before installing new".

Khosrow answered 5/4, 2009 at 23:2 Comment(2)
Don't use $INSTDIR. Use the value you read from the registry to make sure it's the right one.Dunton
Wont this start the uninstall in the background, and allow the user to continue with the installation while the uninstall is happening?Bohrer
D
18

Another approach is to make a UninstallPrevious hidden section and make it run before all other sections in the installer. I also suggest making the uninstaller run silently.

; The "" makes the section hidden.
Section "" SecUninstallPrevious

    Call UninstallPrevious

SectionEnd

Function UninstallPrevious

    ; Check for uninstaller.
    ReadRegStr $R0 HKLM "${HKLM_REG_KEY}" "InstallDir"

    ${If} $R0 == ""        
        Goto Done
    ${EndIf}

    DetailPrint "Removing previous installation."    

    ; Run the uninstaller silently.
    ExecWait '"$R0\Uninstall.exe /S"'

    Done:

FunctionEnd

The advantage of this approach is that the user won't uninstall the old version until they're ready to install the new version. Furthermore, they don't even have to make a decision about uninstalling the old version, it just magically disappears.

Of course, depending on your needs, you may want the user to confirm uninstalling, in which case use the spinner_den's approach.

Drobman answered 6/11, 2009 at 20:54 Comment(1)
When you get data into $R0 shouldn't you use it in the ExecWait ?Burmaburman
K
14

NSIS is a great Windows Installer. Here is how I use NSIS to uninstall the current version while installing a new version of the same application. Add the following function to your NSIS script.

Function .onInit

         Exec $INSTDIR\uninst.exe 

FunctionEnd

Also you can check out this link on the NSIS wiki on "Auto-uninstall old before installing new".

Khosrow answered 5/4, 2009 at 23:2 Comment(2)
Don't use $INSTDIR. Use the value you read from the registry to make sure it's the right one.Dunton
Wont this start the uninstall in the background, and allow the user to continue with the installation while the uninstall is happening?Bohrer
H
2

I reuse the UninstallString or QuietUninstallString registry keys that get written during the install to later determine the uninstaller command.

A couple defines at the top:

!define PROJECT_REG_UNINSTALL_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PROJECT_NAME}"
!define PROJECT_UNINSTALL_EXE "uninstall.exe"

In the installer Section:

WriteRegStr HKLM "${PROJECT_REG_UNINSTALL_KEY}" "UninstallString" '"$INSTDIR\${PROJECT_UNINSTALL_EXE}" _?=$INSTDIR'
WriteRegStr HKLM "${PROJECT_REG_UNINSTALL_KEY}" "QuietUninstallString" '"$INSTDIR\${PROJECT_UNINSTALL_EXE}" /S _?=$INSTDIR'

And then in your .onInit grab the registry key value (would exist if your app was already installed this way) and run it:

${If} ${Silent}
    ReadRegStr $R0 HKLM "${PROJECT_REG_UNINSTALL_KEY}" "QuietUninstallString"
${Else}
    ReadRegStr $R0 HKLM "${PROJECT_REG_UNINSTALL_KEY}" "UninstallString"
${EndIf}
ExecWait "$R0"
Hardcore answered 17/10, 2013 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.