title: External command-line child process in C doesn't give output until it ends
body: <p>I'm having strange issues spawning a process, i found almost nothing related to this...</p>
  
  <p>I need to spawn a command-line child process and be capable of getting its output and sending input before it ends.</p>
  
  <p>I can do it fine with cmd.exe, and runs smooth...
  BUT when i try to do it with external command-line applications they doesn't output nothing to my program until end of execution. I can send input, it processes normally, the number of times i want to, and at finish of child i get ALL the accumulated output.</p>
  
  <p>The related code is:</p>
  
  <pre><code>SECURITY_ATTRIBUTES secatr;
  secatr.nLength = sizeof(SECURITY_ATTRIBUTES);
  secatr.bInheritHandle = TRUE;
  secatr.lpSecurityDescriptor = NULL;
  
  CreatePipe(&amp;stdin_read, &amp;stdin_write, &amp;secatr, 0);
  CreatePipe(&amp;stdout_read, &amp;stdout_write, &amp;secatr, 0);
  SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0);
  SetHandleInformation(stdin_write, HANDLE_FLAG_INHERIT, 0);
  
  STARTUPINFO si;
  PROCESS_INFORMATION pi;
  
  memset(&amp;pi, 0, sizeof(PROCESS_INFORMATION));
  memset(&amp;si, 0, sizeof(STARTUPINFO));
  si.cb = sizeof(STARTUPINFO);
  si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  si.wShowWindow = SW_HIDE;
  si.hStdError = si.hStdOutput = stdout_write;
  si.hStdInput = stdin_read;
  
  CreateProcess(NULL,c:\\\\app.exe, NULL, NULL, TRUE, 0, NULL, "c:\\\\", &amp;si, &amp;pi);
  handleproceso = pi.hProcess;
  handlethread = (HANDLE)_beginthreadex(NULL,0,&amp;Read,NULL,0,NULL);
  _beginthreadex(NULL,0,&amp;Write,NULL,0,NULL);
  
  WaitForSingleObject(pi.hProcess, INFINITE);
  CloseHandle( pi.hProcess );
  CloseHandle( pi.hThread );
  
  
  
  unsigned __stdcall Read(void *a){
  char buff[100];
  
  while(1){
      memset(buff,0,sizeof(buff));
      ReadFile( stdout_read, buff, sizeof(buff)-1, &amp;dwRead, NULL);
      send(socketcliente,buff,strlen(buff),0);
      if(exit1){
          exit1 = false;
          break;
      }
  }
  return 0;
  }
  
  
  
  unsigned __stdcall Write(void *a){
  char ddd[100];
  
  while(1){
      memset(ddd,0,sizeof(ddd));
      if(recv(socketcliente,ddd,sizeof(ddd)-1,0) == SOCKET_ERROR){
          CloseHandle(handlethread);
          TerminateProcess(handleproceso,1);
          break;
      }
      WriteFile(stdin_write, ddd, strlen(ddd), &amp;dwWritten, NULL);
      if(strncmp(ddd,"exit",4) == 0){
          exit1 = true;
          //CloseHandle(handlethread);
          break;
      }
  }
  return 0;
  }
  </code></pre>
  
  <p>I tried the MSDN example of <a href="https://support.microsoft.com/en-us/kb/190351" rel="nofollow">How to spawn console processes with redirected standard handles</a> , with the child process fflushing output buffer it receives the output fine...if I remove that fflush i get the same problem of other programs.</p>
  
  <p>The portion of code looks as:</p>
  
  <pre><code>printf("Child echoing [%s]\n",szInput);
       fflush(NULL);  // Must flush output buffers or else redirection
                      // will be problematic.
  </code></pre>
  
  <p>What can I do if the program I want to spawn doesn't behave that way?Can I force its output or something?</p>
  
  <p>I cannot find any response to that in the whole internet...Thank you in advance and apologies for my basic english.</p>