How can I redirect stdout to some visible display in a Windows Application?

35,360

Solution 1

You need to create pipe (with CreatePipe()), then attach stdout to it's write end with SetStdHandle(), then you can read from pipe's read end with ReadFile() and put text you get from there anywhere you like.

Solution 2

You can redirect stdout, stderr and stdin using freopen.

From the above link:

/* freopen example: redirecting stdout */
#include <stdio.h>

int main ()
{
  freopen ("myfile.txt","w",stdout);
  printf ("This sentence is redirected to a file.");
  fclose (stdout);
  return 0;
}

You can also run your program via command prompt like so:

a.exe > stdout.txt 2> stderr.txt

Solution 3

When you create a process using CreateProcess() you can choose a HANDLE to which stdout and stderr are going to be written. This HANDLE can be a file to which you direct the output.

This will let you use the code without recompiling it. Just execute it and instead of using system() or whatnot, use CreateProcess().

The HANDLE you give to CreateProcess() can also be that of a pipe you created, and then you can read from the pipe and do something else with the data.

Solution 4

You could do something like this with cout or cerr:

// open a file stream
ofstream out("filename");
// save cout's stream buffer
streambuf *sb = cout.rdbuf();
// point cout's stream buffer to that of the open file
cout.rdbuf(out.rdbuf());
// now you can print to file by writing to cout
cout << "Hello, world!";
// restore cout's buffer back
cout.rdbuf(sb);

Or, you can do that with a std::stringstream or some other class derived from std::ostream.

To redirect stdout, you'd need to reopen the file handle. This thread has some ideas of this nature.

Solution 5

This is what I'd do:

  1. CreatePipe().
  2. CreateProcess() with the handle from CreatePipe() used as stdout for the new process.
  3. Create a timer or a thread that calls ReadFile() on that handle every now and then and puts the data read into a text-box or whatnot.
Share:
35,360
JoeBieg
Author by

JoeBieg

Updated on November 27, 2020

Comments

  • JoeBieg
    JoeBieg over 3 years

    I have access to a third party library that does "good stuff." It issues status and progress messages to stdout. In a Console application I can see these messages just fine. In a Windows application they just go to the bit bucket.

    Is there a fairly simple way to redirect stdout and stderr to a text control or other visible place. Ideally, this would not require any recompiles of the third party code. It would just intercept the steams at a low level. I'd like a solution where I just #include the header, call the initialization function and link the library as in...

    #include "redirectStdFiles.h"
    
    void function(args...)
    {
      TextControl* text = new TextControl(args...);
      initializeRedirectLibrary(text, ...);
    
      printf("Message that will show up in the TextControl\n");
      std::cout << "Another message that also shows up in TextControl\n";
    }
    

    Even better would be if it used some interface that I could override so it is not tied to any particular GUI library.

    class StdFilesRedirector
    {
      public:
        writeStdout(std::string const& message) = 0;
        writeStderr(std::string const& errorMessage) = 0;
        readStdin(std::string &putReadStringHere) = 0;
    };
    

    Am I just dreaming? Or does anyone know of something that can do something like this?

    Edit after two answers: I think using freopen to redirect the files is a good first step. For a complete solution there would need to be a new thread created to read the file and display the output. For debugging, doing a 'tail -f' in a cygwin shell window would be enough. For a more polished application... Which is what I want to write... there would be some extra work to create the thread, etc.