GetExitCodeProcess() 使用应用程序验证程序抛出第一次机会异常

GetExitCodeProcess() throws first chance exception with Application Verifier

我使用 CreateProcess() 来 运行 一个可执行文件,并在最后调用 GetExitCodeProcess() 来获取进程的退出代码。当我 运行 它与应用程序验证程序一起使用时,我得到了第一次机会异常。我附上了日志的图片。

行号 1348 是紧接在 GetExitCodeProcess() 调用之后的行。我不是很熟悉日志格式所以我问的是在 GetExitCodeProcess() 内部抛出的异常在这种情况下我可以忽略它还是我在我的代码中做错了什么因为它指向调用 GetExitCodeProcess 之后的行()?

编辑:我的代码:

  if (!CreateProcess(
    NULL,  // it will get the app name from the next argument
    cString, // command line
    NULL,
    NULL,
    TRUE, // don't inherit handles
    CREATE_NO_WINDOW,
    NULL, // use environment of the calling process
    NULL, // use same current drive and directory as the calling process
    &siStartupInfo,
    &processInfo
    ))
  {
    // If process failed return error
    ...
  }

  // Close handles the passed to the child process.
  CloseHandle(hChildStd_ERR_Wr);
  CloseHandle(hChildStd_OUT_Wr);

  // Now read the std::out and std::err from child process and store them in strings to return
  DWORD dwRead;
  CHAR chBuf[4096];
  memset(chBuf, 0, 4096);
  BOOL bSuccess = FALSE;
  UString out = "", err = "";
  for (;;) {
    bSuccess=ReadFile( hChildStd_OUT_Rd, chBuf, 4096, &dwRead, NULL);
    if( ! bSuccess || dwRead == 0 )
      break;

    UString s(chBuf, dwRead);
    out += s;
  }
  dwRead = 0;
  memset(chBuf, 0, 4096);
  for (;;) {
    bSuccess=ReadFile( hChildStd_ERR_Rd, chBuf, 4096, &dwRead, NULL);
    if( ! bSuccess || dwRead == 0 )
      break;

    UString s(chBuf, dwRead);
    err.append(s);
  }

  consoleOutput = out;
  errorOutput = err;

  // Wait for the process to finish. Wait shouldn't happen before reading from pipes because
  // pipe writes happen only when someone is reading from them. For small amounts of data
  // this would not be a problem because the Pipe Manager buffer will consume data so the
  // write will succeed. But for large amounts of data, a reader must be present if not
  // the writer will wait indefinitely.
  WaitForSingleObject(processInfo.hProcess,INFINITE);

  // Close handles returned from Child process
  CloseHandle(processInfo.hProcess);
  CloseHandle(processInfo.hThread);

  // Close read handles of the pipe
  CloseHandle(hChildStd_ERR_Rd);
  CloseHandle(hChildStd_OUT_Rd);

  // Get exit code for the process
  DWORD exitStat = 0;
  GetExitCodeProcess(processInfo.hProcess,&exitStat);

您在 GetExitCodeProcess() 之后调用 您已经关闭了 hProcess 句柄,因此您向其传递了一个无效句柄,这正是验证者的错误日志告诉你什么:

Invalid handle exception for current stack trace

您需要让 hProcess 打开,直到您完全使用它,包括将它传递给 GetExitCodeProcess()