简体   繁体   English

为什么ReadFile()不返回0? 程序试图永远从管道读取数据

[英]Why ReadFile() doesn't return 0 ? Program tries to read data from pipe forever

code: 码:

This is from much larger MS example ( http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx ). 这是来自更大的MS示例( http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx )。 And of course I tried the whole example before, that my snipped below is where I stripped it down to the problem. 当然,我之前尝试了整个例子,我在下面的剪辑是我剥离问题的地方。 The thing is how I'm supposed to break out of this loop if all I get is 1 . 如果我得到的只是1那么我应该如何突破这个循环。 How to do signal that there is no more data to read ? 如何发出没有更多数据要读取的信号?

(Compiler is MiGW.) (编译器是MiGW。)

It looks like the MS example is broken, this is one of the comments there: Parent process needs to close handles that it passes to child process after the CreateProcess() call. 看起来MS示例已损坏,这是其中一条注释: 父进程需要关闭它在CreateProcess()调用后传递给子进程的句柄。 Not doing so will cause the parent to wait forever on ReadFile(). 不这样做会导致父进程在ReadFile()上永远等待。

#include <iostream>
#include <windows.h>
using namespace std;

int main() { 
  cout << "\n->Start of parent execution.\n";
  SECURITY_ATTRIBUTES saAttr;
  saAttr.bInheritHandle = TRUE;
  saAttr.lpSecurityDescriptor = NULL;
  HANDLE g_hChildStd_OUT_Rd = NULL;
  HANDLE g_hChildStd_OUT_Wr = NULL;
  CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0);

  STARTUPINFO siStartInfo;
  PROCESS_INFORMATION piProcInfo;

  ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
  siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
  siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

  CreateProcess(NULL,
    (char*)"cmd /c dir",     // command line
    NULL,          // process security attributes
    NULL,          // primary thread security attributes
    TRUE,          // handles are inherited
    0,             // creation flags
    NULL,          // use parent's environment
    NULL,          // use parent's current directory
    &siStartInfo,  // STARTUPINFO pointer
    &piProcInfo);  // receives PROCESS_INFORMATION


  CHAR chBuf[4096];
  DWORD dwRead;

  // ~~~~~~this loop here~~~~~- how to break out of it ?
  while(true) {
    cout << "ReadFile: " << ReadFile( g_hChildStd_OUT_Rd, chBuf, 4096, &dwRead, NULL) << endl;
  }

  cout << "\n->End of parent execution.\n";
}

Did you try with all the code? 你尝试过所有的代码吗? MSDN has some handle inheritance stuff. MSDN有一些句柄继承的东西。 Looks like the write end of the pipe is not closed, so ReadFile thinks there will be more to read if it waits. 看起来管道的写端没有关闭,因此ReadFile认为如果它等待会有更多的东西要读。

EDIT: 编辑:

"CloseHandle(g_hChildStd_OUT_Wr);" “CloseHandle的(g_hChildStd_OUT_Wr);” makes the read terminate once its read everything, so definitely seems that the issue is the write end never gets fully closed after the child process terminates. 一旦读取了所有内容,就会使读取终止,所以看起来问题似乎是在子进程终止后,write end永远不会完全关闭。 However the MSDN sample doesn't seem to require it, so you would need to look more carefully at why that is the case. 但是,MSDN示例似乎并不需要它,因此您需要更仔细地查看为什么会出现这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM