Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does "printf" write in a Windows Non-console Application?

Tags:

If I choose to create a Windows Non-console Application, and implement printf/cout in the code, where does the printf/cout write? Does it write to the stdout buffer? If yes, is there any way to read it from stdout and print it to some text file or do a MessageBox with the text (just to verify that I have read it correctly)?

EDIT:: Just to clarify that I do not want to redirect the output anywhere. I would like to know, where does printf/cout write? And if it writes to some default buffer, is there a way to read the output, just to verify that I have read the correct output and from the correct buffer. Please, do not give me solutions to redirect the "stdout".

like image 414
Abhineet Avatar asked Feb 08 '14 13:02

Abhineet


People also ask

Where does printf print to in C?

The printf function formats and prints a series of characters and values to the standard output stream, stdout .

Can you use printf to write to a file?

You can use fopen to open a file and then use fprintf and other functions from the standard library to write to the file.

Why printf in C is not working?

Printf is not the thing being buffered, it's stdio, so all similar functions will behave in the same way. To demonstrate the buffering, all you have to do is printf a lot of characters, usually more than 1024, and printf will print as you will have exceeded the maximum buffer length and it will automatically fflush.


2 Answers

Under Windows stdout is a wrapper to the low-level functions accessing the handle returned by GetStdHandle(STD_OUTPUT_HANDLE).

When starting a non-console application by double-clicking (I tried under Windows 7, 64 bit) then GetStdHandle(STD_OUTPUT_HANDLE) will return an invalid handle. This means that printf and other commands will write nothing at all but the low-level functions internally called by printf will return an error code.

However as already said even a non-console program can be started the following way:

program_name > output_file.txt 

In this case the printf output will be written to the output file.

-- EDIT --

If you wish to "catch" the printf() output and write it to MessageBox() there are two ways to achieve this:

The first one is running the program twice while the input of one instance is the standard output of the other one. The behavior could be explained by the following command line:

program_name | program_name /msgbox 

The second possibility works without running two programs and without running the program twice: You may hook file handle #1. This should be at least possible when using msvcrt.dll:

HANDLE hRead,hWrite;  CreatePipe(&hRead,&hWrite,NULL,0); dup2(_open_osfhandle(hWrite,O_WRONLY),1);  // Now printf() output can be read from handle hRead  printf("Hello world 2!\n");  // In a separate thread do: ReadFile(hRead,...); 
like image 77
Martin Rosenau Avatar answered Sep 21 '22 06:09

Martin Rosenau


Since your question seems to be just for information, a windows app without a console, has its stdout, stderr handles closed. Any function that tries to output to those handles, simply gets called, checks for an open handle, finds it closed, and returns without doing anything else.

You might say, your output in this case ends up nowhere to be found :)

If you want to read this output, then you need to open the handles either by allocating a console, or use one of the methods described here.

like image 24
DNT Avatar answered Sep 23 '22 06:09

DNT