CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2010
    Posts
    1

    Reading stdout within MFC

    Hello everybody, this is my first post here. I'm been scouring the internets trying to find a solution to my problem. I have an MFC application, and I would like to be able to read stdout into a buffer so that I can do things with the output. The reason being, I am linking to libs that use stdout to print debug information, and I have no way of controlling where those libs write. So, I've tried the whole CreatePipe, SetSTDHandle, thing, but I get a stall when I go to ReadFile on the stream. My code looks something like:

    HANDLE read;
    HANDLE write;
    CreatePipe(&read, &write, 0, 8192);
    bool b = SetStdHandle( STD_OUTPUT_HANDLE, write );
    b = SetStdHandle( STD_ERROR_HANDLE, write );
    printf("ceci n'est pas une pipe");
    printf("and more!");
    fflush(stdout);
    FlushFileBuffers(read);
    FlushFileBuffers(write);
    DWORD bytes_read;
    b = ReadFile(read, buffer, 1000, &bytes_read, NULL );

    Everything seem to be returning true except when it comes to the ReadFile. The app will just hang there forever and never return. Anyone know how to get around this or what's going on?

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Reading stdout within MFC

    Yes, ReadFile is a blocking function. It doesn't return unless there is an error, or the specified number of bytes have been read (or, if you use it asynchronously; I believe that has to do with overlapped I/O).

    Viggy
    Last edited by MrViggy; June 21st, 2010 at 11:50 AM.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Reading stdout within MFC

    As for me, it would be more natural to dump the stdout to a file rather than a buffer.
    Attached Files Attached Files
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured