title: Trying to use CreateProcess to output a file, but hangs on output?
body: <p>For part of a project, I have to be able to output a file through stdout using CreateProcess. However, it seems to be waiting for input after it outputs. It works when I just run source.exe by itself. </p>
  
  <p>Main Program:</p>
  
  <pre><code>#include &lt;winbase.h&gt;
  
  #define DELAY_A_WHILE() {volatile int j; for (j=0; j &lt; 10000000; ++j) ; }
  #define BUFSIZE 4096;
  
  int main(int argc, char *argv[])
  {
      ///VARS///
  
      char cmdline1[MAX_PATH]; //Stores the command line for source.
      char cmdline2[MAX_PATH]; //Stores the command line for sink.
  
  
      STARTUPINFO StartupInfo; //Stores the startup information.
      PROCESS_INFORMATION ProcInfoSource, ProcInfoFilter, ProcInfoSink; // Filled in by CreateProcess
      // New Process Creation Flags:
      DWORD CreateFlags = 0;
      BOOL createStatus;
  
      HANDLE sourceFilterRead, sourceFilterWrite, filterSinkRead, filterSinkWrite; //Handles for the two pipes.
  
  
      //////////
      //SECURITY_ATTRIBUTES PipeAttributes; //Pipe security attributes.
      //PipeAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
      //PipeAttributes.lpSecurityDescriptor = NULL; // ignore
      //PipeAttributes.bInheritHandle = TRUE; // child can inherit
      //////////
          //////////SOURCE//
      if (argc == 3)
      {
          strcpy(cmdline1, "Source.exe");
          strcat(cmdline1, " ");
          strcat(cmdline1, argv[1]); //Parse the first command line for source.exe.
  
  
          strcpy(cmdline2, "sink.exe");
          strcat(cmdline2, " ");
          strcat(cmdline2, argv[2]); //Parse the second command line for sink.exe.
  
          //if (!CreatePipe(&amp;sourceFilterRead, &amp;sourceFilterWrite, &amp;PipeAttributes, 0)) { //Source | Filter
          //  printf("Error creating pipe: %d\n", GetLastError());
          //}
  
          ///STARTUP///
          GetStartupInfo(&amp;StartupInfo); 
      /*  StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
          StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
          StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); 
          printf("%s\n", cmdline1);
          printf("%s\n", cmdline2);
          */
  
          createStatus = CreateProcess( NULL, cmdline1, NULL, NULL, TRUE, 0, NULL, NULL, &amp;StartupInfo, &amp;ProcInfoSource);
          if(createStatus == 0)
          {
              printf("Source process failed.\n");
              return(-1);
          }
  
          CloseHandle(ProcInfoSource.hProcess);
          CloseHandle(ProcInfoSource.hThread);
          printf("Source success.\n");
      }
      else
          printf("Arg error\n");
      return(0);
  }
  </code></pre>
  
  <p>Source.exe:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;winbase.h&gt;
  #define DELAY_A_WHILE() {volatile int j; for(j=0; j&lt;10000000; j++);}
  
  int main(int argc, char *argv[])
  {
  
      int ch;
      FILE *fin;
      if(argc != 2 || fopen_s(&amp;fin, argv[1], "rb"))
      {
              fprintf(stderr, "Usage: source inputfilename\n");
              return(1);
      }
  
      //Read chars and copy to stdout.
  
      while(1)
      {
          if ((ch = fgetc(fin)) == EOF)
              break;
          fputc(ch, stdout);
  
          DELAY_A_WHILE();
      }
  
      fclose(fin);
      return(0);
  }
  </code></pre>
  
  <p>EDIT: I found a solution. If I use WaitForSingleObject() right after I start the process, it works properly. Not sure if there's a way to do it without it. The goal of the assignment is to have a bunch of processes working simultaneously without waiting for one another, but I have not found another way to do it without waiting for source.exe to finish.</p>