【问题标题】:Getting Batch Script Error Code获取批处理脚本错误代码
【发布时间】:2009-04-20 18:54:54
【问题描述】:

我希望 Win32 进程启动外部脚本并能够检索它返回的 ERRORLEVEL。

反之亦然。只需在您的 Win32 应用程序中使用 exit(errorcode)return errorcode;,调用脚本就会正确设置其 ERRORLEVEL 值。

但是,从 Win32 调用应用程序中,获取脚本错误代码并不完全相同。

我尝试使用 CreateProcess() 并调用 GetExitCodeProcess(),但它总是返回 0 而不是 ERRORLEVEL 的实际值。即使我用 exit %ERRORLEVEL%

结束我的调用脚本

我最好的猜测是脚本不是一个进程。很可能 CMD.EXE 正在运行,并且很可能总是以 ExitCode 0 结束。我知道 ERRORLEVEL 与进程 ExitCode 值不同,我希望 CMD.EXE 会镜像它。

编辑:

对不起,我问了!我刚刚发现我的问题。我在批处理文件中使用 exit /b errorcode 而不是 exit errorcode。当您从命令行进行测试时,似乎 /b 选项的优点是只关闭正在运行的脚本,而不是 CMD.EXE。但是没有为 CMD.EXE 设置正确的 ExitCode 的缺点。

所以,为了后代,我正在做的是:

int LaunchScript(TCHAR *pCmdLineParam)
{
    bool bResult;
    PROCESS_INFORMATION pi;
    STARTUPINFO si;

    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);

    TCHAR   cmdLine[256];
    _tcscpy(cmdLine,L"Test.cmd");
    _tcscat(cmdLine,L" ");
    _tcscat(cmdLine,pCmdLineParam);

    _tprintf(L"Process executing: %s\n",cmdLine);

    bResult = CreateProcess(NULL, cmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)?true:false;
    if (!bResult) {
        _tprintf(L"CreateProcess() error - %d", GetLastError());
        return -1;
    }

    DWORD result = WaitForSingleObject(pi.hProcess,15000);
    if( result == WAIT_TIMEOUT ) {
        return -2;
    }

    DWORD exitCode=0;
    if( !GetExitCodeProcess(pi.hProcess,&exitCode) ) {
        _tprintf(L"GetExitCodeProcess() error - %d", GetLastError());
        return -1;
    }

    _tprintf(L"Process exitcode=%d\n",exitCode);

    return exitCode;
}

我的“入口点”批处理文件如下所示:

@call %*
@exit %ERRORLEVEL%

我将我的脚本作为参数传递给入口点脚本。其他“子脚本”文件可以调用 exit /b 或 exit,因为所有内容都已涵盖。

【问题讨论】:

    标签: winapi batch-file


    【解决方案1】:

    这对我有用:

    int DoDOS(string parms)
    {
    
    Process p=new Process();
    ProcessStartInfo ProcInfo=new ProcessStartInfo();
    
    ProcInfo.Arguments="/C "+parms;
    ProcInfo.CreateNoWindow=true;
    ProcInfo.ErrorDialog=false;
    ProcInfo.ErrorDialogParentHandle=IntPtr.Zero;
    ProcInfo.FileName="cmd.exe";
    ProcInfo.RedirectStandardError=false;
    ProcInfo.RedirectStandardInput=false;
    ProcInfo.RedirectStandardOutput=false;
    ProcInfo.UseShellExecute=false;
    ProcInfo.Verb="";
    ProcInfo.WindowStyle=ProcessWindowStyle.Hidden;
    p=Process.Start(ProcInfo);
    
    while (!p.HasExited)
          {
          if (laRunning.Text!=RunningTxt) laRunning.Text=RunningTxt;
          else                            laRunning.Text="";
          Application.DoEvents();
          Thread.Sleep(500);
          }
    
    return p.ExitCode; 
    }
    

    【讨论】:

      【解决方案2】:

      只是在黑暗中拍摄(因为我在家,周围没有窗户):

      我们在工作中使用'cmd /c call <script>'。也许它适用于您的问题。

      【讨论】:

      • 嗯……不错。我错过了,因为我尝试不使用“呼叫”但它不起作用。
      【解决方案3】:

      尝试将此作为CreateProcesslpCommandLine 参数传递:

      cmd /v:on /k <script_name> & exit !errorlevel!
      

      它将开启延迟的environment variable expansion(否则%ERRORLEVEL%在执行&lt;script_name&gt;之前会扩展)并显式返回脚本返回的ERRORLEVEL作为cmd.exe的返回码.

      【讨论】:

        猜你喜欢
        • 2011-11-01
        • 2011-03-27
        • 1970-01-01
        • 2014-05-21
        • 1970-01-01
        • 2020-11-29
        • 1970-01-01
        • 1970-01-01
        • 2015-01-19
        相关资源
        最近更新 更多