在保持连接打开的同时多次读取匿名管道 (WinAPI)
所以我一直在命名管道和匿名管道之间来回切换,这就是我的问题。我尝试了命名管道,但它们似乎无法正常工作,无法满足我的需求,所以我又回到了匿名管道。但是,匿名管道需要从管道(到我未创建的程序)读取输入,并随着管道可用的更多信息而不断读取它。这是我当前拥有的相关代码:
void Arc_Redirect::createProcesses()
{
TCHAR programName[]=TEXT("program.exe");
PROCESS_INFORMATION pi;
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if(!CreatePipe(&outStd[0].hOutRead,&outStd[0].hOutWrite,&sa,0) || !CreatePipe(&outStd[0].hInRead,&outStd[0].hInWrite,&sa,0))
throw "Error: Could not CreatePipe();!";
if(!SetHandleInformation(outStd[0].hOutRead,HANDLE_FLAG_INHERIT,0) || !SetHandleInformation(outStd[0].hInWrite,HANDLE_FLAG_INHERIT,0))
throw "Error: Could not SetHandleInformation();";
// Set stuff up
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.hStdError = outStd[0].hOutWrite;
si.hStdOutput = outStd[0].hOutWrite;
si.hStdInput = outStd[0].hInRead;
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
outStd[0].o1.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(!CreateProcess(programName,NULL,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
throw "Error: Could not start Program!";
// Cleanup the useless handles
if(!CloseHandle(pi.hThread) || !CloseHandle(pi.hProcess))
throw "Error: Could not CloseHandle();";
}
这是我读取管道的方式:
void Arc_Redirect::readPipe()
{
CHAR chBuf[BUFSIZE];
DWORD dwRead;
ReadFile(outStd[0].hOutRead,chBuf,sizeof(chBuf),&dwRead,&outStd[0].o1);
chBuf[dwRead] = '\0';
SetDlgItemText(global,IDO_WORLDOUT,chBuf);
ResetEvent(outStd[0].o1.hEvent);
}
BUFSIZE 定义在 0x1000 处,outStd 定义为 PIPE_HANDLES (结构如下)
typedef struct
{
HANDLE hOutRead;
HANDLE hOutWrite;
HANDLE hInRead;
HANDLE hInWrite;
OVERLAPPED o1;
TCHAR chReq[BUFSIZE];
TCHAR chReply[BUFSIZE];
DWORD dwRead;
DWORD dwWritten;
DWORD dwState;
DWORD cbRet;
BOOL pendingIO;
} PIPE_HANDLES, *LPSTDPIPE;
现在,我可以正确调用 readPipe();一次,它正是我想要的。但是,如果我尝试再次调用它,就会失败。关于如何解决这个问题有什么想法吗?就像我说的,我需要保持连接打开并增量读取。为了便于讨论,假设我需要每 5 秒读一次;我正在读取的程序每 5 秒就会有不同的输出需要读取。有什么帮助吗?
问候,
丹尼斯·M.
So I keep bouncing between named and anonymous pipes and here is my issue. I tried named pipes and they just didn't seem to work properly for what I wanted, so I'm back to anonymous pipes. However, the anonymous pipe needs to read input from a pipe (to a program that I did not create) and continuously read it as more information is available to the pipe. Here is the relevant code which I currently have:
void Arc_Redirect::createProcesses()
{
TCHAR programName[]=TEXT("program.exe");
PROCESS_INFORMATION pi;
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if(!CreatePipe(&outStd[0].hOutRead,&outStd[0].hOutWrite,&sa,0) || !CreatePipe(&outStd[0].hInRead,&outStd[0].hInWrite,&sa,0))
throw "Error: Could not CreatePipe();!";
if(!SetHandleInformation(outStd[0].hOutRead,HANDLE_FLAG_INHERIT,0) || !SetHandleInformation(outStd[0].hInWrite,HANDLE_FLAG_INHERIT,0))
throw "Error: Could not SetHandleInformation();";
// Set stuff up
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.hStdError = outStd[0].hOutWrite;
si.hStdOutput = outStd[0].hOutWrite;
si.hStdInput = outStd[0].hInRead;
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
outStd[0].o1.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(!CreateProcess(programName,NULL,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
throw "Error: Could not start Program!";
// Cleanup the useless handles
if(!CloseHandle(pi.hThread) || !CloseHandle(pi.hProcess))
throw "Error: Could not CloseHandle();";
}
And here is how I am reading the pipe:
void Arc_Redirect::readPipe()
{
CHAR chBuf[BUFSIZE];
DWORD dwRead;
ReadFile(outStd[0].hOutRead,chBuf,sizeof(chBuf),&dwRead,&outStd[0].o1);
chBuf[dwRead] = '\0';
SetDlgItemText(global,IDO_WORLDOUT,chBuf);
ResetEvent(outStd[0].o1.hEvent);
}
BUFSIZE is defined at 0x1000 and outStd is defined as PIPE_HANDLES (struct below)
typedef struct
{
HANDLE hOutRead;
HANDLE hOutWrite;
HANDLE hInRead;
HANDLE hInWrite;
OVERLAPPED o1;
TCHAR chReq[BUFSIZE];
TCHAR chReply[BUFSIZE];
DWORD dwRead;
DWORD dwWritten;
DWORD dwState;
DWORD cbRet;
BOOL pendingIO;
} PIPE_HANDLES, *LPSTDPIPE;
Now, I can properly call readPipe(); once and it does exactly what I want. However, if I try to call it again, it fails. Any ideas on how I can fix this issue? Like I said, I need to keep the connection open and read incrementally. For sake of argument, say I need to read every 5 secs; the program I am reading from will have different output every 5 secs that needs to be read. Any help?
Regards,
Dennis M.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不找出原因,就不可能猜出为什么会失败。 ReadFile 失败时返回 FALSE。然后您应该调用 GetLastError() 来获取诊断信息。
永远不要忽略 Windows API 返回值。至少断言它们。
It is impossible to guess why it fails if you don't find out why. ReadFile returns FALSE when it fails. You should then call GetLastError() to get a diagnostic.
Never ignore Windows API return values. At the very least assert them.