2017-04-13 325 views
0

在这个小NSIS安装,我想在内部设置错误级别和外部抓住它(当我运行一个批处理脚本中静默模式安装/卸载),但不知何故,我总是%ERRORLEVEL% = 0NSIS静默卸载SetErrorLevel

这里是我的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 

有什么不对的呢?我认为,这些线(在错误的情况下)我应该%ERRORLEVEL%= 1

SetErrorLevel 1 
Quit 

例如:安装后,我特意删除注册表项“HKLM \ SOFTWARE \ $ {MY_APP_NAME}” ,然后运行卸载程序。它没有找到问题的关键和退出的预期,但%ERRORLEVEL%还是0

回答

1

安装程序应设置代码预期。

卸载程序在%temp%中执行自身的一个副本,以便它能够在$Instdir中删除自己。如果失败,它将设置非零退出代码,但不会等待另一个卸载程序实例并报告真正的退出代码。

您可以用documented _?=开关跳过复制步骤运行卸载程序但你必须手动删除卸载程序.exe文件。

_?=设置$ INSTDIR。它还会阻止卸载程序将自身复制到临时目录并从那里运行。它可以与ExecWait一起用于等待卸载程序完成。它必须是命令行中使用的最后一个参数,并且不得包含任何引号,即使路径包含空格也是如此。