如何从Windows命令行获取应用程序退出代码?

我正在运行一个程序,并希望看到它的返回码是什么(因为它根据不同的错误返回不同的代码)。

我知道在Bash我可以通过跑步来做到这一点

echo $?

在Windows上使用cmd.exe时该怎么办?

名为errorlevel伪环境variables存储退出代码:

 echo Exit Code is %errorlevel% 

另外, if命令有一个特殊的语法:

 if errorlevel 

看看if /? 了解详情。

 @echo off my_nify_exe.exe if errorlevel 1 ( echo Failure Reason Given is %errorlevel% exit /b %errorlevel% ) 

警告:如果您设置环境variables名称errorlevel ,则%errorlevel%将返回该值,而不是退出代码。 使用(set errorlevel= )清除环境variables,允许通过%errorlevel%环境variables访问errorlevel的真实值。

testingErrorLevel适用于控制台应用程序,但正如dmihailescu所暗示的dmihailescu ,如果您尝试从命令提示符运行窗口化应用程序(例如基于Win32的应用程序),则这将不起作用。 窗口化的应用程序将在后台运行,控制将立即返回到命令提示符(很可能ErrorLevel为零,表示进程已成功创build )。 当窗口应用程序最终退出时,其退出状态将丢失。

但是,不要使用其他地方提到的基于控制台的C ++启动程序,而是使用命令提示符的START /WAIT命令来启动一个带窗口的应用程序。 这将启动窗口化的应用程序,等待它退出,然后将控制权返回到命令提示符,并在ErrorLevel中设置进程的退出状态。

 start /wait something.exe echo %errorlevel% 

使用内置的ERRORLEVELvariables:

 echo %ERRORLEVEL% 

但要小心应用程序是否定义了名为ERRORLEVEL的环境variables !

当使用未连接到控制台的程序时,它可能无法正常工作,因为该应用程序可能仍在运行,而您认为您有退出代码。 在C ++中的解决scheme如下所示:

 #include "stdafx.h" #include "windows.h" #include "stdio.h" #include "tchar.h" #include "stdio.h" #include "shellapi.h" int _tmain( int argc, TCHAR *argv[] ) { CString cmdline(GetCommandLineW()); cmdline.TrimLeft('\"'); CString self(argv[0]); self.Trim('\"'); CString args = cmdline.Mid(self.GetLength()+1); args.TrimLeft(_T("\" ")); printf("Arguments passed: '%ws'\n",args); STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); if( argc < 2 ) { printf("Usage: %s arg1,arg2....\n", argv[0]); return -1; } CString strCmd(args); // Start the child process. if( !CreateProcess( NULL, // No module name (use command line) (LPTSTR)(strCmd.GetString()), // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi ) // Pointer to PROCESS_INFORMATION structure ) { printf( "CreateProcess failed (%d)\n", GetLastError() ); return GetLastError(); } else printf( "Waiting for \"%ws\" to exit.....\n", strCmd ); // Wait until child process exits. WaitForSingleObject( pi.hProcess, INFINITE ); int result = -1; if(!GetExitCodeProcess(pi.hProcess,(LPDWORD)&result)) { printf("GetExitCodeProcess() failed (%d)\n", GetLastError() ); } else printf("The exit code for '%ws' is %d\n",(LPTSTR)(strCmd.GetString()), result ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); return result; } 

如果要准确匹配错误代码(例如等于0),请使用以下命令:

 @echo off my_nify_exe.exe if %ERRORLEVEL% EQU 0 ( echo Success ) else ( echo Failure Reason Given is %errorlevel% exit /b %errorlevel% ) 

“如果错误级别0”匹配errorlevel> = 0,请参阅“if /?”。

有一点我需要准确地将日志事件从Cygwin推到Windows事件日志。 我想WEVL中的消息是自定义的,有正确的退出代码,细节,优先级,消息等等。所以我创build了一个Bash脚本来处理这个问题。 这里是在GitHub上, logit.sh 。

一些摘录:

 usage: logit.sh [-h] [-p] [-i=n] [-s] <description> example: logit.sh -p error -i 501 -s myscript.sh "failed to run the mount command" 

这里是临时文件内容部分:

 LGT_TEMP_FILE="$(mktemp --suffix .cmd)" cat<<EOF>$LGT_TEMP_FILE @echo off set LGT_EXITCODE="$LGT_ID" exit /b %LGT_ID% EOF unix2dos "$LGT_TEMP_FILE" 

这是一个在WEVL中创build事件的函数:

 __create_event () { local cmd="eventcreate /ID $LGT_ID /L Application /SO $LGT_SOURCE /T $LGT_PRIORITY /D " if [[ "$1" == *';'* ]]; then local IFS=';' for i in "$1"; do $cmd "$i" &>/dev/null done else $cmd "$LGT_DESC" &>/dev/null fi } 

执行批处理脚本并调用__create_event:

 cmd /c "$(cygpath -wa "$LGT_TEMP_FILE")" __create_event