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.