windows - NSIS中止和错误级别

标签 windows error-handling installation nsis abort

我正在用NSIS构建一个简单的安装程序,并希望在批处理脚本中以静默模式运行它,最后我将像这样检查ERRORLEVEL:

REM a simple test
REM get into folder and run the installer
cd /d my_installer_path 
my_installer_made_with_nsis.exe /S
if %ERRORLEVEL% NEQ 0 echo something has gone wrong

现在,似乎my_installer_made_with_nsis.exe总是返回%ERRORLEVEL% 0,即使在nsi代码内部,我明确设置了错误级别并退出Abort。

在我的nsi脚本中,我使用像这样的ErrorHandling函数
Function ErrorHandler
IfSilent +2 0
MessageBox MB_ICONSTOP "$err_msg"

DetailPrint "$err_msg"
SetErrorlevel 5 ; <---- APPARENTLY DOES NOT WORK
Abort
FunctionEnd

然后,在我的代码中,我这样调用错误处理程序:
...
StrCpy $err_msg "An exception occurred: blah blah"
Call ErrorHandler

我希望在cmd shell中,%ERRORLEVEL%为5,但始终为0。

也许我弄错了NSIS中ErrorLevel的整个概念,但是在那种情况下,是否可以从命令 shell 中检索安装程序的退出代码?

最佳答案

SetErrorlevel可以正常工作,这意味着它可以设置进程的退出代码。 %ERRORLEVEL%不一定是子进程的退出代码,尤其是在调用GUI进程时。您的问题出在您的批处理文件/cmd.exe,而不是NSIS。

Function MyAbortFunction
SetErrorlevel 5
Abort
FunctionEnd

Section
System::Call 'Kernel32::GetEnvironmentVariable(t "TestAbort", p 0, i0)i.r0'
${If} $0 = 0
    System::Call 'Kernel32::SetEnvironmentVariable(t "TestAbort", t "1")'
    ExecWait '"$ExePath"' $0 ; Run the test instance of our self
    MessageBox MB_OK "Exit code = $0" ; This will be 5
    SetErrorlevel $0 ; Make the parent use the same code in case you are testing in cmd.exe
${Else}
    Call MyAbortFunction
${EndIf}
SectionEnd

在cmd.exe中,它可以正常工作:
start "" /WAIT "c:\my\path\to\Test.exe"
echo %errorlevel%

关于windows - NSIS中止和错误级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44784663/

相关文章:

c - 我该如何禁用关闭按钮?

c - 使用 ShellExecuteEx 永久设置路径变量

windows - 如何获取 Windows SYSTEM 用户 token

c++ - 为什么添加两个空格会产生错误的输出?

linux - 在没有 sudo 访问权限的情况下安装 perl 模块

windows - Mac 设置以在每种可能的浏览器中测试应用程序?

python - 使用Python发送带有输出日志和成功状态的电子邮件

c# - 返回错误信息 vs 抛出异常

java - 将 .jar 文件提取到目录,添加新文件,然后在 java 中重新打包成 .jar 文件

sql-server - 安装 SQL Server 2017 Developer 版本时 Oracle JRE 规则失败