7

I've recently encountered the following problem with my application: it didn't show any console output, though the console had been allocated by using AllocConsole. I managed to figure out soon that it was caused by an attempt (hidden deeply in code) to write to the console before the AllocConsole was called. So it looked like this:

Console.WriteLine("Foo"); // no console allocated yet
AllocConsole();           // console window appears
Console.WriteLine("Bar"); // expecting "Bar" in the console, but the console is blank

So my question is: why does this happen? I don't see any exceptions (though I suppose they are there).

Dmitrii Erokhin
  • 1,347
  • 13
  • 31
  • Now that why it happened has been explained, I hope someone will explain what to do about it. Isn't that any third-party library is use can write to Console and screw up things for all others? Is the right solution only to decide early and `AllocConsole` before doing anything else? – Miserable Variable Sep 24 '11 at 06:16
  • @Hemal I used an approach, described by Rick - calling `Console.OpenStandartOutput()` after `AllocConsole`. This way I can be sure that at least all output written after `AllocConsole` will be shown. – Dmitrii Erokhin Sep 24 '11 at 06:22

3 Answers3

12

The first time you use Console.WriteLine, the Console class creates a TextWriter and associates it with the Console.Out property. The way it does this is to use Win32 to open the low-level file handle associated with the standard output file handle. If the standard output handle is invalid, Console.Out is set to TextWriter.Null, which discards all output.

The Win32 AllocConsole function, creates and sets the standard output handle so after calling it the standard output handle is either different or now valid. In either case, Console.Out has already been set either to use the old standard output or to discard all output.

To force a re-open of Console.Out after calling AllocConsole, you can use this method:

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • Wow. In case anyone else see this, it seems that this can happen as if it's a race condition-- Alloc, then write to Console & get nothing. Also, I had to write Console.SetOut(new StreamWriter(Console.OpenStandardOutput()); Calling Console.OpenStandardOutput() alone didn't help. – MatthewMartin Apr 17 '13 at 23:09
1

Probably because the static constructor of the Console class sets up the output stream the first time you call Console.WriteLine. Since there's no console attached, and therefore no standard output handle, output gets routed to the bit bucket. And when you call AllocConsole later, nothing in the Console class is notified that a console now exists. It doesn't have the opportunity to attach Console.Out to the newly created standard output handle.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0

A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. And the console application is already has the console. See details in here

Ujjwal Manandhar
  • 2,194
  • 16
  • 20
  • My app is compiled as a GUI application, but it uses the console when run using command line parameters. – Dmitrii Erokhin Sep 24 '11 at 05:53
  • How do you know that this is a console application? It may very well be a Windows application with a console. *Edit:* told you. – BoltClock Sep 24 '11 at 05:54
  • Console.WriteLine was writen above the AllocConsole so i thought it was. any way i apologize for this :) GetLastError() might tell you something in this case – Ujjwal Manandhar Sep 24 '11 at 05:57