0

Is it possible for any windows console app to output to console, while not outputting to stdout nor stderr? If yes - how to capture/redirect the output?

I have a third party console app that I used to start from my C# app and redirect its stdandard error and output and read them from within my app. With an upgrade of this 3rd party app this approach stopped working. During investigation I found out that while it outputs to console; redirecting stderr to file caused it not to output to console (expected), but file was empty (very unexpected):

CNTK.exe 1>out.txt 2>err.txt

No content in out.txt nor err.txt

Without redirection the app outputs the basic diagnostic info into the console.

Based on the source code it uses fprintf(stderr, ...) for outputing - so I'm quite puzzled why it's not redirectable. Even regardless of the app in question or how it outputs to console - I'm quite puzzled how it is even possible for the app to output to console without user ability to redirect the output to pipes.

Edit: seems to be caused by a bug in older version of MS cpp runtime: https://connect.microsoft.com/VisualStudio/feedback/details/1441507/stdout-and-stderr-not-always-flushed-upon-process-exit 3rd party app is linked against VS 2013 runtime and codepath executed by my test was not explicitly calling fflush(stderr); prior exiting (despite this should not be needed, but it is due to the bug)

Jan
  • 1,905
  • 17
  • 41
  • 1
    Your last question is easily answered: you can open the `CON` device or the `CONOUT$` device to write to the current console. But in this case my best guess would be that the program exits without first flushing the output buffers. – Harry Johnston Jan 09 '17 at 20:56
  • @HarryJohnston - unless the app crashes (which is not the case); buffer should be flushed by the OS, shouldn't it? – Jan Jan 10 '17 at 06:56
  • @HarryJohnston You are probably very correct - I found the MS connect issue about stderr not being properly flushed on app clean exit: https://connect.microsoft.com/VisualStudio/feedback/details/1441507/stdout-and-stderr-not-always-flushed-upon-process-exit Feel free to post your comment as answer and I'll accept it – Jan Jan 10 '17 at 10:13
  • *buffer should be flushed by the OS, shouldn't it?* - the OS buffers will be flushed by the OS, but the C runtime buffers won't. My guess is that you're *not* seeing the same problem described in that bug report, though since I can't see the source code for either of them it's impossible to be certain. – Harry Johnston Jan 10 '17 at 20:35
  • My thought was that the program might be exiting without telling the C runtime library, e.g., using `ExitProcess` rather than `exit`, which will consistently produce the results you describe if the program is statically linked to the C runtime, i.e., built with /MT rather than /MD. There are other possible causes, e.g., if the program is silently crashing when it attempts to exit. You might be able to narrow this down by stepping through the exit sequence in a debugger. – Harry Johnston Jan 10 '17 at 20:43
  • (The thing about the bug report is that it says it only happens about one time in a hundred, and if I'm reading your question correctly your problem happens every time. It could still be the same underlying cause exhibiting in different ways, but it makes it less likely.) – Harry Johnston Jan 10 '17 at 20:46

0 Answers0