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 6086487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:45:40+00:00 2026-05-23T11:45:40+00:00

I have followed the example here for reading from a pipe, but ReadFile fails

  • 0

I have followed the example here for reading from a pipe, but ReadFile fails and GetLastError() shows me that the pipe is broken.
I have created and used (successfully) a pipe earlier in the program, but I closed all the handles and used entirely new variables for the new pipe just to be sure.
Any ideas why this doesn’t work?

HANDLE g_hChildStd_OUT_Rd2 = NULL;
HANDLE g_hChildStd_OUT_Wr2 = NULL;
SECURITY_ATTRIBUTES saAttr2; 
STARTUPINFO si2;
PROCESS_INFORMATION pi2;

ZeroMemory( &si2, sizeof(si2) );
si2.cb = sizeof(si2);
ZeroMemory( &pi2, sizeof(pi2) );
//create pipe
saAttr2.nLength = sizeof(SECURITY_ATTRIBUTES); 
saAttr2.bInheritHandle = TRUE; 
saAttr2.lpSecurityDescriptor = NULL; 
CreatePipe(&g_hChildStd_OUT_Rd2, &g_hChildStd_OUT_Wr2, &saAttr2, 0);
//create child process
bSuccess = FALSE;
memset(szCmdLine, 0, MAX_PATH);
sprintf(szCmdLine, "ffmpeg.exe -i output.mp3");
ZeroMemory( &pi2, sizeof(PROCESS_INFORMATION) );
ZeroMemory( &si2, sizeof(STARTUPINFO) );
si2.cb = sizeof(STARTUPINFO); 
si2.hStdOutput = g_hChildStd_OUT_Wr2;
si2.dwFlags |= STARTF_USESTDHANDLES;
CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si2, &pi2);
//read from pipe
CloseHandle(g_hChildStd_OUT_Wr2);
memset(chBuf, 0, BUFSIZE);
for (;;) 
{ 
  bSuccess = ReadFile( g_hChildStd_OUT_Rd2, chBuf, BUFSIZE, &dwRead, NULL);
  [bSuccess is 0 and GetLastError() returns error 109]
  ........
  • 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-23T11:45:40+00:00Added an answer on May 23, 2026 at 11:45 am

    “Broken pipe” is a normal error when the other end closes the pipe. In your case, this either means there is no “other end”, or the other application hasn’t written anything to its stdout.
    I’ve modified your code to be a compilable test case – there are two cases in which ReadFile fails without having read any data for me:

    • CreateProcess failed. I’ve added an assert so that problem will be recognizable. It would of course result in a broken pipe.
    • The process does not write anything to standard output. It might instead write to its standard error stream. I’ve redirected stderr to the pipe as well, and as my echo example shows, this will receive both streams on the other end of the pipe now.

    Code:

    #define WIN32_LEAN_AND_MEAN
    #include <Windows.h>
    #include <assert.h>
    #include <stdio.h>
    #include <string.h>
    
    #define BUFSIZE 200
    
    int main(void)
    {
        BOOL bSuccess;
        char szCmdLine[MAX_PATH];
        char chBuf[BUFSIZE];
        DWORD dwRead;
        HANDLE g_hChildStd_OUT_Rd2 = NULL;
        HANDLE g_hChildStd_OUT_Wr2 = NULL;
        SECURITY_ATTRIBUTES saAttr2; 
        STARTUPINFO si2;
        PROCESS_INFORMATION pi2;
    
        ZeroMemory( &si2, sizeof(si2) );
        si2.cb = sizeof(si2);
        ZeroMemory( &pi2, sizeof(pi2) );
        //create pipe
        saAttr2.nLength = sizeof(SECURITY_ATTRIBUTES); 
        saAttr2.bInheritHandle = TRUE; 
        saAttr2.lpSecurityDescriptor = NULL; 
        assert(CreatePipe(&g_hChildStd_OUT_Rd2, &g_hChildStd_OUT_Wr2, &saAttr2, 0));
        //create child process
        bSuccess = FALSE;
        memset(szCmdLine, 0, MAX_PATH);
        sprintf(szCmdLine, "cmd /c echo output && echo error>&2");
        ZeroMemory( &pi2, sizeof(PROCESS_INFORMATION) );
        ZeroMemory( &si2, sizeof(STARTUPINFO) );
        si2.cb = sizeof(STARTUPINFO); 
        si2.hStdOutput = g_hChildStd_OUT_Wr2;
        si2.hStdError = g_hChildStd_OUT_Wr2; // also add the pipe as stderr!
        si2.dwFlags |= STARTF_USESTDHANDLES;
        assert(CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si2, &pi2));
        //read from pipe
        CloseHandle(g_hChildStd_OUT_Wr2);
        memset(chBuf, 0, BUFSIZE);
        for (;;) 
        { 
            bSuccess = ReadFile( g_hChildStd_OUT_Rd2, chBuf, BUFSIZE, &dwRead, NULL);
            printf("%d %lu 0x%08lx\n", bSuccess, dwRead, GetLastError());
            if (bSuccess)
                printf("\t'%*s'\n", (int)dwRead, chBuf);
            else
                break;
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have followed the onTouch example from google located here . However, I get
I have followed the code example here toupper c++ example And implemented it in
I've just followed this example from Wordpress and I have successfully added an extra
I have followed all the instructions here: http://www.tonyspencer.com/2003/10/22/curl-with-php-and-apache-on-windows/ to install & config apache get
I have followed this tutorial which allowed me to create a Silverlight DataGrid that
I have followed the example in for the gradient dividers: http://www.connorgarvey.com/blog/?p=34 I have tried
I have a Sencha Touch App that loads data from a REST service into
I have set up the spellchecker for the example installation configuration that comes with
Here's the situation: I have a label's text set, immediately followed by a response.redirect()
I have followed this example. However, when I port it to my phone (in

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.