views:

28

answers:

1

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.

A: 

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.

Hans Passant
ok I set BOOL bSuccess = ReadFile() and added the following condition: if(!bSuccess) throw GetLastError(); However, it still works the first time, but the second time it never makes it to the throw statement but the program freezes probably meaning ReadFile() is hanging. Any ideas? And yes, my try{...}catch(){...} statement is properly setup to catch the errors from my readPipe(); function (I tested).
RageD
I just switched to multi-threading. It makes this work as I require. Thanks for your help!
RageD