如何使用NSIS安装较新版本的软件之前先卸载较旧版本的软件

时间:2018-11-06 10:09:20

标签: nsis

我正在使用NSIS创建新的软件安装程序。在使用NSIS之前,我们曾使用WIX安装程序来创建相同的安装程序包。

我的要求是,当我们使用NSIS安装较新版本的安装程序(例如EMR 4.0)时,如果存在任何用WIX安装程序创建的较旧版本的安装程序(例如EMR 3.0),则应该在安装新版本的安装程序之前,请先删除较旧的安装程序。

为此,我在.Oninit中编写了以下代码段,但它不起作用

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

在上一行中,我传递的第一个参数来自Windows注册表。

第二个参数“ 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"

这里将转到其他部分并显示消息: “读取注册表C:\ Program Files \ CPS \ PowerChute Personal Edition \”

然后我添加了以下代码,但在IDOK处显示错误:“无效的命令:“ IDOK”

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

我想知道,这是卸载的正确方法吗? “ Exec $ R0 \ uninst.exe”行真的有效吗?

我们还可以使用$ {If} $ R0 <>“”检查路径是否为空吗?

请帮助我,请提出您的想法。

下面是更新的代码:

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}

是否可以在If条件下,直接在MessageBox下方使用ExecWait'“ $ R0 \ uninst.exe”'

已更新鳕鱼以适应新要求:

如果IF条件为true,则在下面的.onInit函数中,而不是显示MessageBox,这是我的新要求。我需要显示一个对话框,在其中我有一个标题,如“更改,修复或删除安装”,“删除”按钮和一些文字。当我单击“删除”按钮时,它应该会卸载。我已经写了一些代码,但是请帮助我完成它。

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

1 个答案:

答案 0 :(得分:0)

ReadRegStr不会压入堆栈,Pop $0毫无意义。如果您想知道如何检测指令是否失败,应查阅文档。 ReadRegStr设置错误标志,并在失败时返回""(空字符串)。

IDOK不是指令,而是与MessageBox一起使用的关键字。从某个地方复制代码时,您可能犯了一个错误。

Exec $R0\uninst.exe可能有效,具体取决于该卸载程序的编写方式,没有通用答案。但是,我建议您改用ExecWait '"$R0\uninst.exe"'

由于<>是数字,因此不能使用<> ""来检查字符串是否为空,请使用${If} $R0 != ""LogicLib.nsh列出了可用的运算符。

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}
相关问题