Console out from a non-console app

We found out something interesting yesterday: Windows applications can write to STDOUT.

Well, not when you’re using Delphi. If a Delphi/Win32 app tries to WriteLn(), it will get an exception, saying, in effect, “You idiot, you don’t have a console to write to!”

On the other hand, if you’re coding in C# (or even Delphi for .NET), and you do a Console.WriteLine (or Console.Out.WriteLine or Console.Error.WriteLine), you don’t get an error. I had always assumed that was just a convenience, so you could put debug output into your code, and then not have to remove it when you compile that code into a Windows app. I had always assumed that Console.Out went to Stream.Null when you were in a Windows application.

Nope. Go try it. Go into Visual Studio and create a new WinForms app. Make a button-click event that does Console.Out(). Build. Then go to the command prompt and type

WindowsApplication1 >out.txt

Run it. Click the button. Exit. And look at out.txt. Your output will be there. Cool, huh?

(Later I’ll blog about how we found this, and what we’re using it for. Unless Brian or Sam beats me to it.)

Leave a Reply

Your email address will not be published. Required fields are marked *