Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6686101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:07:59+00:00 2026-05-26T05:07:59+00:00

I am trying to create a child process running any exe command and redirect

  • 0

I am trying to create a child process running any exe command and redirect all its stdio and stderr to my parent process through an anonymous pipe. But when my parent process tries to do a ReadFile() on the READ end of the anonymous pipe after the child has terminated, it just blocks instead of returning an error.

Here is the code:

#include "windows.h"
//==================================================
void createChildPipes(PHANDLE phRead, PHANDLE phWrite){
  SECURITY_ATTRIBUTES sa={sizeof(SECURITY_ATTRIBUTES) ,NULL,TRUE};
  if ( !CreatePipe(phRead, phWrite, &sa, 2048) ) { //...  }
}
//==================================================
void setupStartupInfo(LPSTARTUPINFO lpsi, HANDLE hStdOutput, HANDLE hStdError) {
  lpsi->cb = sizeof(STARTUPINFO);
  lpsi->lpReserved=NULL;
  lpsi->lpDesktop=NULL;
  lpsi->lpTitle=NULL;
  lpsi->dwX=0;
  lpsi->dwY=0;
  lpsi->dwXSize=200;
  lpsi->dwYSize=500;
  lpsi->dwFlags=STARTF_USESTDHANDLES;
  lpsi->cbReserved2=0;
  lpsi->lpReserved2=NULL;
  lpsi->hStdInput=GetStdHandle(STD_INPUT_HANDLE);
  lpsi->hStdError= hStdError;
  lpsi->hStdOutput=hStdOutput;
}
//==================================================
void createChildProcess(PHANDLE phOutRead, PHANDLE phOutWrite, PHANDLE phErrRead, PHANDLE phErrWrite) {
  TCHAR name[]=_T("cl.exe");
  createChildPipes(phOutRead, phOutWrite);
  STARTUPINFO si;
  setupStartupInfo(&si, *phOutWrite, *phOutWrite);
  PROCESS_INFORMATION pi;
  if (!CreateProcess(NULL, name, NULL,  NULL,   true, 0,   NULL,  NULL,  &si,  &pi)) { //...}
}
//==================================================
void _tmain(int argc, _TCHAR* argv[]){
  HANDLE hOutRead, hOutWrite, hErrRead, hErrWrite;
  createChildProcess(&hOutRead, &hOutWrite, &hErrRead, &hErrWrite) ;
  char buf[10];

  BOOL readState;
  for (;;) {
    DWORD dwBytesRead;
    memset(buf, '\0', sizeof(buf));
    readState=ReadFile(hOutRead, buf, sizeof(buf)-2 , &dwBytesRead, NULL);
    printf("%s", buf);
    if (!readState)
      break;
  } 

}

This is what my code is doing. My _tmain() creates a child process (I am using the VC++ cl.exe command as a test) and redirects its stdio and stderr to the write HANDLE of an anonymous pipe. My parent process reads from the read HANDLE of the same pipe.

These are printed by my _tmain() showing that the pipe can communicate across the parent-child processes. These are what we expect to see if we type cl.exe in the command line without any arguments. Note the peculiar behaviour of cl.exe that the first 2 lines are from the stderr of cl.exe and the last line is from the stdout.

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

Now about the problem, the line:

readState=ReadFile(hOutRead, buf, sizeof(buf)-2 , &dwBytesRead, NULL);

in _tmain() blocks after the child cl.exe process terminated. But, I am expecting the ReadFile() call to return with an ERROR_BROKEN_PIPE error status and exit the parent loop instead of blocking the ReadFile() call.

Why the ReadFile() blocks when reading an anonymous pipe?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:08:00+00:00Added an answer on May 26, 2026 at 5:08 am

    You forgot to close the pipe handles in the parent process, so the pipe is not completely closed yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a child process in another process. I am writing both
I'm trying to create a function that searches up a WebControl's parent-child relationship (basically
I am trying to create a child process and then send SIGINT to the
I am trying to create Below parent child structur using WPF tree. Tree ->Parent
I'm trying to create a two-way communication between parent and child processes using 2
I'm trying to create a child form that helps the user to enter data
I am trying to create Linq Expression that I can use to query child
So I have a program which creates a child process and executes a command
I am trying to create a (child) JFrame which slides out from underneath one
I am trying to create a parent WPF/surface application which will host multiple WPF/surface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.