Q: Usage of anon. pipes for communication w/ child process

Q: Usage of anon. pipes for communication w/ child process

Post by Dominik Herrman » Fri, 15 Sep 2000 00:07:51



Please help me - I have a really difficult problem.
I want to write a programm which makes usage of a console telnet client in
Win32.
After having created a new process with the child app, my application will
write to the child's console and read the output afterwards with anonymous
pipes. Unfortunately, I seem to make something wrong.

I user code like this to create the child console app:
---
BOOL CreateHiddenConsoleProcess(LPCTSTR szChildName, PROCESS_INFORMATION*
ppi,
                                LPHANDLE phInWrite, LPHANDLE phOutRead,
                                LPHANDLE phErrRead) {
    BOOL fCreated;
    STARTUPINFO si;
    SECURITY_ATTRIBUTES sa;
    HANDLE hInRead;
    HANDLE hOutWrite;
    HANDLE hErrWrite;

    // Create pipes
    // initialize security attributes for handle inheritance (for WinNT)
    sa.nLength = sizeof( sa );
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor  = NULL;

    // create STDIN pipe
    if( !CreatePipe( &hInRead, phInWrite, &sa, 0 ))
        goto error;

    // create STDOUT pipe
    if( !CreatePipe( phOutRead, &hOutWrite, &sa, 0 ))
        goto error;

    // create STDERR pipe
    if( !CreatePipe( phErrRead, &hErrWrite, &sa, 0 ))
        goto error;

    // process startup information
    memset( &si, 0, sizeof( si ));
    si.cb = sizeof( si );
    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    // child process' console must be hidden for Win95 compatibility
    si.wShowWindow = SW_SHOWNORMAL;
    // assign "other" sides of pipes
    si.hStdInput = hInRead;
    si.hStdOutput = hOutWrite;
    si.hStdError = hErrWrite;

    // Create a child process (suspended)
    fCreated = CreateProcess( NULL,
                              (LPTSTR)szChildName,
                              NULL,
                              NULL,
                              TRUE,
                              0,
                              NULL,
                              NULL,
                              &si,
                              ppi );

    if( !fCreated )
        goto error;

    CloseHandle( hInRead );
    CloseHandle( hOutWrite );
    CloseHandle( hErrWrite );

    return TRUE;

error:
//error handling //
    return FALSE;

Quote:}

---

This seems to work fine. The main problem is, that I am not able to go one
step further.
My code to read from the console:
---
if(!ReadFile(hOutRead, buf, size, &read, NULL))
  int i=GetLastError();
---
ReadFile fails and i is set to 109 - which is ERROR_BROKEN_PIPE.

What is my mistake?
Is there another possibility to communicate with a console process?

Thank you for your help!

Dominik