gpt4 book ai didi

windows - NSIS中止和错误级别

转载 作者:行者123 更新时间:2023-12-03 08:11:24 24 4
gpt4 key购买 nike

我正在用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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com