Joe White’s Blog

Life, .NET, and Cats

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.)

There are no responses to “Console out from a non-console app” yet.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Joe White's Blog copyright © 2004-2011. Portions of the site layout use Yahoo! YUI Reset, Fonts, and Grids.
Proudly powered by WordPress. Entries (RSS) and Comments (RSS). Privacy policy