m_Input=5; UpdateData(TRUE); HANDLE hChildStdinRd, hChildStdinWr;
HANDLE hChildStdoutRd, hChildStdoutWr;   SECURITY_ATTRIBUTES saAttr; 
   BOOL fSuccess; 
 
// Set the bInheritHandle flag so pipe handles are inherited. 
 
   saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
   saAttr.bInheritHandle = TRUE; 
   saAttr.lpSecurityDescriptor = NULL; 
 
 
   if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) 
   {
   MessageBox("创建stdin管道失败!");
   return;
   }
 // Create a pipe for the child process's STDOUT. 
 
   if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) 
   {
   MessageBox("创建stdout管道失败!");
   return;
   }   PROCESS_INFORMATION piProcInfo; 
   STARTUPINFO siStartInfo;
   BOOL bFuncRetn = FALSE; 
 
// Set up members of the PROCESS_INFORMATION structure. 
 
   ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
 
// Set up members of the STARTUPINFO structure. 
 
   ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
   siStartInfo.cb = sizeof(STARTUPINFO); 
   siStartInfo.hStdError = hChildStdoutWr;
   siStartInfo.hStdOutput = hChildStdoutWr;
   siStartInfo.hStdInput = hChildStdinRd;
   siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
 
// Create the child process. 
    
   bFuncRetn = CreateProcess(NULL, 
   "D:/软件代码和资料/Mofat程序和资料/Mofat/release/test4pipe.exe",       // command line 
      NULL,          // process security attributes 
      NULL,          // primary thread security attributes 
      TRUE,          // handles are inherited 
      0,             // creation flags 
      NULL,          // use parent's environment 
      NULL,          // use parent's current directory 
      &siStartInfo,  // STARTUPINFO pointer 
      &piProcInfo);  // receives PROCESS_INFORMATION 
   
   if (bFuncRetn == 0) 
   {
   MessageBox("创建进程失败!");
   return;
   }
/*
   else 
   {
      CloseHandle(piProcInfo.hProcess);
      CloseHandle(piProcInfo.hThread);
      return;
   }
*/   const int BUFSIZE=1024;
   DWORD dwRead, dwWritten; 
   CHAR chBuf[BUFSIZE]; // Close the write end of the pipe before reading from the 
// read end of the pipe. 
 
//   if (!CloseHandle(hStdoutWr)) 
//      ErrorExit("CloseHandle failed"); 
 
// Read output from the child process, and write to parent's STDOUT. 
 
//   for (;;) 
   { 
      fSuccess=ReadFile( hChildStdoutRd, chBuf, 5, &dwRead, NULL);
  if(fSuccess)
  {
  CString strTmp;
  strTmp.Format("%s",chBuf);
  m_Output+=strTmp;
  Invalidate();
  }   } 

解决方案 »

  1.   

    你的这些代码没什么问题,问题可能在于你的test4pipe.exe进程如何使用从父进程继承的管道。
      

  2.   

    给你一个参考例子,父子进程是如何使用管道的
    父进程
    #include <windows.h> 
    #include <tchar.h>
    #include <stdio.h> 
     
    #define BUFSIZE 4096 
     
    HANDLE hChildStdinRd, hChildStdinWr,  
       hChildStdoutRd, hChildStdoutWr, 
       hInputFile, hStdout;
     
    BOOL CreateChildProcess(VOID); 
    VOID WriteToPipe(VOID); 
    VOID ReadFromPipe(VOID); 
    VOID ErrorExit(LPSTR); 
     
    int _tmain(int argc, TCHAR *argv[]) 

       SECURITY_ATTRIBUTES saAttr; 
       BOOL fSuccess; 
     
    // Set the bInheritHandle flag so pipe handles are inherited. 
     
       saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
       saAttr.bInheritHandle = TRUE; 
       saAttr.lpSecurityDescriptor = NULL; // Get the handle to the current STDOUT. 
     
       hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
     
    // Create a pipe for the child process's STDOUT. 
     
       if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) 
          ErrorExit("Stdout pipe creation failed\n"); // Ensure the read handle to the pipe for STDOUT is not inherited.   SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0);// Create a pipe for the child process's STDIN. 
     
       if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) 
          ErrorExit("Stdin pipe creation failed\n"); // Ensure the write handle to the pipe for STDIN is not inherited. 
     
       SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0);
     
    // Now create the child process. 
       
       fSuccess = CreateChildProcess();
       if (! fSuccess) 
          ErrorExit("Create process failed with"); // Get a handle to the parent's input file. 
     
       if (argc == 1) 
          ErrorExit("Please specify an input file");    printf( "\nContents of %s:\n\n", argv[1]);   hInputFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, 
          OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);    if (hInputFile == INVALID_HANDLE_VALUE) 
          ErrorExit("CreateFile failed"); 
     
    // Write to pipe that is the standard input for a child process. 
     
       WriteToPipe(); 
     
    // Read from pipe that is the standard output for child process. 
     
       ReadFromPipe(); 
     
       return 0; 

     
    BOOL CreateChildProcess() 

       TCHAR szCmdline[]=TEXT("child");
       PROCESS_INFORMATION piProcInfo; 
       STARTUPINFO siStartInfo;
       BOOL bFuncRetn = FALSE; 
     
    // Set up members of the PROCESS_INFORMATION structure. 
     
       ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
     
    // Set up members of the STARTUPINFO structure. 
     
       ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
       siStartInfo.cb = sizeof(STARTUPINFO); 
       siStartInfo.hStdError = hChildStdoutWr;
       siStartInfo.hStdOutput = hChildStdoutWr;
       siStartInfo.hStdInput = hChildStdinRd;
       siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
     
    // Create the child process. 
        
       bFuncRetn = CreateProcess(NULL, 
          szCmdline,     // command line 
          NULL,          // process security attributes 
          NULL,          // primary thread security attributes 
          TRUE,          // handles are inherited 
          0,             // creation flags 
          NULL,          // use parent's environment 
          NULL,          // use parent's current directory 
          &siStartInfo,  // STARTUPINFO pointer 
          &piProcInfo);  // receives PROCESS_INFORMATION 
       
       if (bFuncRetn == 0) 
          ErrorExit("CreateProcess failed\n");
       else 
       {
          CloseHandle(piProcInfo.hProcess);
          CloseHandle(piProcInfo.hThread);
          return bFuncRetn;
       }
    }
     
    VOID WriteToPipe(VOID) 

       DWORD dwRead, dwWritten; 
       CHAR chBuf[BUFSIZE]; 
     
    // Read from a file and write its contents to a pipe. 
     
       for (;;) 
       { 
          if (! ReadFile(hInputFile, chBuf, BUFSIZE, &dwRead, NULL) || 
             dwRead == 0) break; 
          if (! WriteFile(hChildStdinWr, chBuf, dwRead, 
             &dwWritten, NULL)) break; 
       } 
     
    // Close the pipe handle so the child process stops reading. 
     
       if (! CloseHandle(hChildStdinWr)) 
          ErrorExit("Close pipe failed\n"); 

     
    VOID ReadFromPipe(VOID) 

       DWORD dwRead, dwWritten; 
       CHAR chBuf[BUFSIZE]; // Close the write end of the pipe before reading from the 
    // read end of the pipe. 
     
       if (!CloseHandle(hChildStdoutWr)) 
          ErrorExit("Closing handle failed"); 
     
    // Read output from the child process, and write to parent's STDOUT. 
     
       for (;;) 
       { 
          if( !ReadFile( hChildStdoutRd, chBuf, BUFSIZE, &dwRead, 
             NULL) || dwRead == 0) break; 
          if (! WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL)) 
             break; 
       } 

     
    VOID ErrorExit (LPSTR lpszMessage) 

       fprintf(stderr, "%s\n", lpszMessage); 
       ExitProcess(0); 
    }子进程
    #include <windows.h> #define BUFSIZE 4096 
     
    VOID main(VOID) 

       CHAR chBuf[BUFSIZE]; 
       DWORD dwRead, dwWritten; 
       HANDLE hStdin, hStdout; 
       BOOL fSuccess; 
     
       hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
       hStdin = GetStdHandle(STD_INPUT_HANDLE); 
       if ((hStdout == INVALID_HANDLE_VALUE) || 
          (hStdin == INVALID_HANDLE_VALUE)) 
          ExitProcess(1); 
     
       for (;;) 
       { 
       // Read from standard input. 
          fSuccess = ReadFile(hStdin, chBuf, BUFSIZE, &dwRead, NULL); 
          if (! fSuccess || dwRead == 0) 
             break; 
     
       // Write to standard output. 
          fSuccess = WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL); 
          if (! fSuccess) 
             break; 
       } 
    }
      

  3.   

    谢谢tttyd,这个例子我也已经看了
    我调用的是一个写好的程序,我只是想从我的程序中把输入传给它并获得它的输出
      

  4.   

    我在创建进程的时候已经把子进程的标准输入和输出从定向到了两个管道的读写端  siStartInfo.hStdOutput = hChildStdoutWr; 
      siStartInfo.hStdInput = hChildStdinRd; 
    不知道有问题吗?
      

  5.   

    你这样传是错的。
    用GetStdHandle()这个函数来传,而且是在子进程里得到父进程的建的句柄
      

  6.   

    我找到问题所在了,谢谢各位的回复。
    我调用的子进程
    首先输出Input a number:
    然后再输入一个数,记为n
    然后输出n次“aaaaaa”我原来以为子进程要先输出Input a number:,此时我调用ReadFile读到buff里,然后再显示到编辑框里,
    然后再用WriteFile给子进程输入n,最后再用ReadFile读其余的输出。我把第一个ReadFile和WriteFile交换一下顺序,就OK了。具体原理不明白,我认为是要先把输入全部写给子进程,它需要的时候就去读,然后我们才能读子进程的输出。不管怎样,要感谢各位,特别是tttyd的关注。