简体   繁体   中英

How to uninstall older version of the software before installing the newer version using NSIS

I am creating the new software installer using NSIS. Before using NSIS we were used WIX installer to created the same installer package.

my requirement is when we are installing newer version of the Installer (for eg EMR 4.0) using NSIS, if there is any older version of installer (for eg EMR 3.0) that was created with WIX installer and is present then we should remove the older installer before installing the new version of the installer.

For this i have written the below code snippet in .Oninit but it is not working

ReadRegStr $R0 HKLM "SOFTWARE\CPS\PowerChute Personal Edition\3.01.00" "InstallPath"

In the above line first parameter i passed is from the windows registry.

Second parameter "InstallPath" is the directory location InstallPath: C:\\Program Files\\CPS\\PowerChute Personal Edition\\

Pop $0;
${If} $0 <> 0
MessageBox MB_ICONSTOP "Reading Registry Failed, error $0"
${Else}
MessageBox MB_OKCANCEL "Reading the Registry $R0"

Here it is going to the Else part and displaying the message: "Reading the Registry C:\\Program Files\\CPS\\PowerChute Personal Edition\\"

Then I have added the below code but it is showing error at IDOK: "Invalid command: "IDOK"

IDOK uninst
    Abort
    ${EndIf}
    uninst:
    Exec $R0\uninst.exe
    done:

I wanted to know, is it the correct way to uninstall? is the line "Exec $R0\\uninst.exe" really works?

And also can we use ${If} $R0 <> "" to check the path is not blank?

Please help me to proceed further by giving your thoughts.

Below is the Updated code:

ReadRegStr $R0 HKLM "SOFTWARE\CPS\PowerChute Personal Edition\3.01.00" "InstallPath"

${If} $R0 != ""
MessageBox MB_OKCANCEL "Powerchute is already installed. Remove the pervious version"
ExecWait '"$R0\uninst.exe"'
${Else}
MessageBox MB_OKCANCEL "Powerchute is not installed"

${EndIf}

Can I use the ExecWait '"$R0\\uninst.exe"' directly below the MessageBox in the If condition.

Updated Cod for the New requirement:

My new requirement is in the below .onInit function if the IF condition is true instead of showing the MessageBox I need to show a Dialog Where i have Title like "Change, repair, or remove installation", "Remove" button and some text. And when I click on "Remove" button, it should uninstall. I have written some code but please help me to finish it.

Page custom MyPageCreate

Function .onInit

ReadRegStr $R0 HKLM "SOFTWARE\APC\PowerChute Personal Edition\3.01.00" "InstallPath"

${If} $R0 != ""
;MessageBox MB_OKCANCEL "Powerchute is already installed in $R0 Remove the ;pervious version?" IDOK uninst

; HERE I SHOULD CALL THE FUNCTION
MyPageCreate

Abort
uninst:

ExecWait '"MsiExec.exe" /X{8ED262EE-FC73-47A9-BB86-D92223246881}'

${Else}
MessageBox MB_OKCANCEL "Not intalled"

${EndIf}

FunctionEnd

Var Text

Below is the function:

Function MyPageCreate
nsDialogs::Create 1018
Pop $0

${NSD_CreateLabel} 0 0 100% 12u "Change, repair, or remove installation"
Pop $0


${NSD_CreateText} 0 13u 100% -13u "Type something here..."
    Pop $Text

**** Here I need to give the remove button **********

nsDialogs::Show
FunctionEnd

ReadRegStr does not push to the stack, Pop $0 is meaningless. You should check the documentation if you want to know how to detect if a instruction failed. ReadRegStr sets the error flag and returns "" (empty string) when it fails.

IDOK is not an instruction but it is a keyword used with MessageBox . You probably made a mistake when you copied code from somewhere.

Exec $R0\\uninst.exe might work, depends on how that uninstaller is written, there is no generic answer. I would however suggest that you use ExecWait '"$R0\\uninst.exe"' instead.

You cannot use <> "" to check if a string is empty since <> is for numbers, use ${If} $R0 != "" . LogicLib.nsh lists the available operators.

ReadRegStr $R0 HKLM "SOFTWARE\CPS\PowerChute Personal Edition\3.01.00" "InstallPath"
${If} $R0 != ""
  MessageBox MB_OKCANCEL "Powerchute is already installed. Remove the pervious version?" IDOK uninst
  Abort
uninst:
  ExecWait '"$R0\uninst.exe"'
${Else}
  MessageBox MB_OK "Powerchute is not installed"
${EndIf}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM