Listing 3: Implementation of WriteToPipe and ReadFromPipe

DWORD CPipedProcess::WriteToPipe(char *command) 
{ 
DWORD dwWritten; 

WriteFile(hChildStdinWrDup, command, strlen(command),
          &dwWritten, NULL);

return dwWritten;
  
} 
 
DWORD CPipedProcess::ReadFromPipe(CString& chBuf)
{ 
DWORD dwRead; 

char *chBuf_;

chBuf.Empty();
if(NULL == (chBuf_ = (char*) calloc(BUFSIZE + 2, sizeof(char))))
 { ErrorExit("Not enough memory to read from pipe");
   return 1;
 }

/* Read output from child, and write it to buffer */ 
do {
if(!ReadFile(hChildStdoutRd, chBuf_, BUFSIZE, &dwRead, NULL) ||
    dwRead == 0)
 ErrorExit("Read output from child failed");

 *(chBuf_ + dwRead) = '\x0';
 chBuf += chBuf_;

}
while (dwRead >= BUFSIZE);

free((char*) chBuf_);

return dwRead;

} 
//End of File