Can I redirect stdin and stdout in a child process?

This is what I use to redirect stderr, stdout to a file from a program I start using DosStartSession. You could do the same type of thing using a pipe.

    ULONG  ulAction;
    ULONG  ulNew;
    HFILE  hfFile, hfNewStdOut = -1, hfNewStdErr = -1,
           hfStdOut = 1, hfStdErr = 2;

    // Open output file
    DosOpen( szOutputFile, &hfFile, &ulAction, 1, 0,
             FILE_OPEN | FILE_CREATE,
             OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE, 0L );
    // Duplicate handles
    DosDupHandle( hfStdOut, phfNewStdOut );
    DosDupHandle( hfStdErr, phfNewStdErr );
    // Close existing handles for current process
    DosClose( hfStdOut );
    DosClose( hfStdErr );
    // Redirect existing handles to new file
    DosDupHandle( hfFile, &hfStdOut );
    DosDupHandle( hfFile, &hfStdErr );
    // Let started program inherit handles from parent
    stdata.InheritOpt = SSF_INHERITOPT_PARENT;
    // Start new session
    DosStartSession( &stdata, &ulSessionID, &pidSession );
    // Get back original handles
    DosDupHandle( hfNewStdOut, &hfStdOut );
    DosDupHandle( hfNewStdErr, &hfStdErr );
    // Close the duplicated handles - no longer needed
    DosClose( hfNewStdOut );
    DosClose( hfNewStdErr );

Credit: Rick Fishman


[Back: Can we use Vio in v2.0? Where are the docs for it?]
[Next: How do I use DosMon*() to stuff the kbd buf?]