We help IT Professionals succeed at work.

Redirecting stdout and stderr of MFC GUI Application

pcommons
pcommons asked
on
5,264 Views
Last Modified: 2013-11-20
I have an MFC GUI application that uses some DLLs that will sometimes write something to stdout or stderr. I would like to capture this output and redirect it to a CEdit control I have in a logging window. Can anyone suggest how to do this?
Comment
Watch Question

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
look at the stdout in the help file and freopen.  
stdout is a FILE* and freopen should allow you to reassign it to another file.  

copied from the help files
/* FREOPEN.C: This program reassigns stderr to the file
 * named FREOPEN.OUT and writes a line to that file.
 */

#include <stdio.h>
#include <stdlib.h>

FILE *stream;

void main( void )
{
   /* Reassign "stderr" to "freopen.out": */
   stream = freopen( "freopen.out", "w", stderr );

   if( stream == NULL )
      fprintf( stdout, "error on freopen\n" );
   else
   {
      fprintf( stream, "This will go to the file 'freopen.out'\n" );
      fprintf( stdout, "successfully reassigned\n" );
      fclose( stream );
   }
   system( "type freopen.out" );
}


Author

Commented:
That is how to redirect it to a new file, I assume. But that was not my question. How do I capture it so I can display it in the GUI?
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Check the file to see if it as changed.  (Every second via a Timer?).  Maybe use a CMemFile as the file output is redirected to.
Read the contents of the file and display in the edit box.

Commented:
I mage an experiement which may help you (though it is not full answer to your question). In MFC dialog-based application I added the following function:

void CSampleDlg::OnButton1()
{
    printf("Sample output\n");      
}

Exe file is called Sample.exe. Now, from command window, I run:

Sample.exe >> out.txt

Dialog application runs. I press Button1, close application and see in out.txt file:

Sample output

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks to everyone for their comments.

AndyAinscow's suggestion to redirect to a file didn't work for me  for some reason. The file "freopen.out" was created but no output other than the code directly sent to stream made it to the file. Not sure why.

AlexFM's idea to change link options to create a console window worked for stderr but not stdout. Not sure why. But this was still not as elegant getting everything into the GUI itself.

_nn_ 's suggestion worked quite well. I duplicated the code for stderr as well and got exactly what I wanted.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Hmm - I copied that from the help file as an example of how to do just that.  Microsoft strikes again!

Author

Commented:
:-) Yeah you gotta love it when the help documentation is wrong. Thanks again to everyone for their help with this.

Commented:
Hey

I am trying to do something similar...

What I would like is to redirect my MFC application stdout to the console window.
The goal is to use the same exe to do console work and GUI.
So if the exe is called with certain parameters it will behave like a console app and sent its output to the console. In other cases it will simply display a GUI and let the user interact with it.

Any one has an idea on how to accomplish that?
Thanks.

Author

Commented:
Not sure what you mean - do you always want a console and sometimes also a GUI and either way (GUI or no GUI) have stdout go to the console? Or something else?

Commented:
Well my application will work as follows:


if there is command line arguments then
{
     redirect the stdout to the console
     ....
}
else
   startGUI()

Author

Commented:
Well if you have the GUI and want the stdout to go to the GUI, the solution is above. If  you would prefer to output data to a console, there is the AllocConsole() command which you can call from the GUI which would create a console window...

Commented:
Well what I really want is to be able to see my printf's appear in the console I used to launch the application.
If possible I do not want to create a new console but use the one the app was run from.

Also keep in mind I am working with an MFC app. I don't know if this makes a difference

Author

Commented:
Ah, I understand. You might want to try the linker options that AlexFM suggests above and see if that does the trick. I must admit I'm not a console expert, hence this question in the first place. :-)

Commented:
I tested my way with printf - it works.

Commented:
the problem if to the linker option trick, my app will always create a window...even if it is running only in GUI mode.

And even if I start if from a console it will probably create a new one.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.