简体   繁体   English

ReadFile挂在管道上阅读

[英]ReadFile hanging on pipe reading

I am using CreateProcess() to run cmd.exe (without the physical window showing to the user) and need to process the output. 我正在使用CreateProcess()来运行cmd.exe (没有向用户显示物理窗口)并需要处理输出。 I have decided to use CreatePipe() for this purpose. 我已决定使用CreatePipe()来实现此目的。

I am currently having an issue whereby all of my output is being read and processed, but the final call to ReadFile() is hanging. 我目前遇到一个问题,即我的所有输出都被读取和处理,但最后调用ReadFile()是挂起的。 Searching around has told me that I need to close the write side of the pipe before reading, and that this is a solution to this problem, but I have already done this and still have the problem. 搜索周围告诉我,我需要在阅读之前关闭管道的写入侧,这是解决这个问题的方法,但我已经完成了这个并且仍然有问题。

Here is my code: 这是我的代码:

// sw -> path to cmd.exe, ptr is the command
ok = CreateProcess(sw, ptr, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo);
CloseHandle(hStdInPipeRead);
char buf[1024 + 1] = {};
DWORD dwRead = 0;
DWORD dwAvailable = 0;
DWORD testRes;
CloseHandle(hStdOutPipeWrite);
ok = ReadFile(hStdOutPipeRead, buf, 1024, &dwRead, NULL);
// String handling for initial read omitted for clarity
string temp = buf;
bool holdOff = false;

while (ok == TRUE)
{
    buf[dwRead] = '\0';
    OutputDebugStringA(buf);
    puts(buf);
    // ReadFile gets all the correct output from cmd here but it also hangs on the very last call. How to fix?
    ok = ReadFile(hStdOutPipeRead, buf, 1024, &dwRead, NULL);
    temp = buf;
    // handle and store output
    break;
}
CloseHandle(hStdOutPipeRead);
CloseHandle(hStdInPipeWrite);

The child process will inherit the handle of pipe if you set SECURITY_ATTRIBUTES.bInheritHandle = true , the write handle of the pipe you closed is only the parent's, there is still a handle in child process(stdout of child), and it hasn't been closed in cihld process, Readfile fails and returns only when all write handles are closed or errors occur. 如果你设置SECURITY_ATTRIBUTES.bInheritHandle = true ,子进程将继承管道的句柄,你关闭的管道的写句柄只是父进程,子进程中仍然有一个句柄(子进程的stdout),它没有在cihld进程中已关闭, Readfile失败并仅在所有写入句柄关闭或发生错误时返回。

In addition, Anonymous Pipe Operations . 此外, 匿名管道运营

Asynchronous (overlapped) read and write operations are not supported by anonymous pipes(Create by CreatePipe ). 匿名管道( CreatePipe创建)不支持异步(重叠)读取和写入操作。

So, If you still need to send command to child process to execute cmd, you should put ReadFile in a thread. 因此,如果您仍需要向子进程发送命令以执行cmd,则应将ReadFile放入线程中。 And if you don't need any more, terminate child process: 如果您不再需要,请终止子进程:

TerminateProcess(ProcessInfo.hProcess,0);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
ok = ReadFile(hStdOutPipeRead, buf, 1024, &dwRead, NULL);
// String handling for initial read omitted for clarity
string temp = buf;
bool holdOff = false;
while (ok == TRUE)
{
    buf[dwRead] = '\0';
    OutputDebugStringA(buf);
    puts(buf);
    // ReadFile gets all the correct output from cmd here but it also hangs on the very last call. How to fix?
    ok = ReadFile(hStdOutPipeRead, buf, 1024, &dwRead, NULL);
    temp = buf;
    // handle and store output
    break;
}
CloseHandle(hStdOutPipeRead);
CloseHandle(hStdInPipeWrite);

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

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