简体   繁体   English

NSIS静默卸载SetErrorLevel

[英]NSIS silent uninstall SetErrorLevel

In this little nsis installer, I would like to internally set the errorlevel and catch it externally (when I run the installer/uninstaller in silent mode inside a batch script), but somehow I always get %errorlevel% = 0. 在这个小小的nsis安装程序中,我想在内部设置错误级别并在外部捕获它(当我在批处理脚本中以静默方式运行安装程序/卸载程序时),但是无论如何,我总是会得到%errorlevel%= 0。

Here is my nsi script 这是我的nsi脚本

!addincludedir .\include
!include StrRep.nsh
!include ReplaceInFile.nsh


!include LogicLib.nsh


!include FileFunc.nsh
!insertmacro GetParameters
!insertmacro GetOptions


!define MY_APP_NAME "foo"

Outfile "${MY_APP_NAME}.exe"

InstallDir $DESKTOP


Section

  ${GetParameters} $R0 


  ClearErrors
  ${GetOptions} $R0 /PLACEHOLDER= $0

  IfErrors 0 +2
    Call ErrorHandler

  SetOutPath $INSTDIR


  File /r foo_root_folder

  !insertmacro _ReplaceInFile "foo_root_folder\subfolder_a\test.properties" "%%placeholder_string%%" "$0"


  WriteRegStr HKLM "SOFTWARE\${MY_APP_NAME}" "Install_Dir" "$INSTDIR\foo_root_folder"


  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${MY_APP_NAME}" "Publisher" "Federico"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${MY_APP_NAME}" "DisplayName" "${MY_APP_NAME}"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${MY_APP_NAME}" "DisplayVersion" "1.0"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${MY_APP_NAME}" "UninstallString" '"$INSTDIR\foo_root_folder\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${MY_APP_NAME}" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${MY_APP_NAME}" "NoRepair" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${MY_APP_NAME}" "EstimatedSize" 1000
  WriteUninstaller "foo_root_folder\uninstall.exe"



SectionEnd

Section "Uninstall"

  ReadRegStr $0 HKLM "SOFTWARE\${MY_APP_NAME}" "Install_Dir"

  ${If} ${Errors}
    Call un.ErrorHandler
  ${Else}
    ${IF} $0 == ""
                Call un.ErrorHandler
          ${ELSE}


          DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${MY_APP_NAME}"
          DeleteRegKey HKLM "SOFTWARE\${MY_APP_NAME}"


          RmDir /r /REBOOTOK "$0"
          ${ENDIF}
  ${EndIf}


SectionEnd



Function ErrorHandler
  SetErrorLevel 1 
  Quit

FunctionEnd


Function un.ErrorHandler
  SetErrorLevel 1 
  Quit

FunctionEnd

Is something wrong in it? 这有什么问题吗? I think that with those lines (in case of error) I should have %errorlevel% = 1 我认为使用这些行(如果出现错误),我应该具有%errorlevel%= 1

SetErrorLevel 1 
Quit

For example: after the installation, I deliberately delete the registry key "HKLM\\SOFTWARE\\${MY_APP_NAME}", and THEN run the uninstaller. 例如:安装后,我故意删除注册表项“ HKLM \\ SOFTWARE \\ $ {MY_APP_NAME}”,然后运行卸载程序。 It does not find the key and quits as expected, BUT the %errorlevel% is still 0 它找不到密钥并按预期退出,但是%errorlevel%仍为0

The installer should set the code as expected. 安装程序应按预期设置代码。

The uninstaller executes a copy of itself in %temp% so that it is able to delete itself in $Instdir . 卸载程序将在%temp%执行自身的副本,以便能够在$Instdir删除自身。 It will set a non-zero exit code if this fails but it will not wait for the other uninstaller instance and report the true exit code. 如果失败,它将设置一个非零的退出代码,但是它不会等待其他卸载程序实例并报告真实的退出代码。

You can run the uninstaller with the documented _?= switch to skip the copy step but then you have to manually delete the uninstaller .exe. 您可以使用已记录的_?=开关运行卸载程序以跳过复制步骤,但随后必须手动删除卸载程序.exe。

_?= sets $INSTDIR. _?=设置$ INSTDIR。 It also stops the uninstaller from copying itself to the temporary directory and running from there. 它还会阻止卸载程序将自身复制到临时目录并从那里运行。 It can be used along with ExecWait to wait for the uninstaller to finish. 它可以与ExecWait一起使用,以等待卸载程序完成。 It must be the last parameter used in the command line and must not contain any quotes, even if the path contains spaces. 它必须是命令行中使用的最后一个参数,并且即使路径包含空格,也不能包含任何引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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