How to avoid crashing win32 app when stdout is unavailable and write is called?

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Apr 26 16:38:26 PDT 2012


So I've just realized that write() throws an exception if stdout is
unavailable and it's called. It throws this:
std.exception.ErrnoException at D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1164):
 (Bad file descriptor)

This happens when an app has a WinMain and is built with
-L-Subsystem:Windows. It usually works ok if it's run from within
another app that redirects stdout, for example if I invoke the app
from Scintilla I will get stdout printed to the screen, but if I
invoke the app from the console then stdout isn't properly set and
calling write throws an exception.

So, how do I redirect write's stdout so it doesn't throw if it doesn't
find a valid stdout handle? Basically I want to turn write/writef into
do-nothing functions if stdout isn't there.

I know ideally I should use logging (still waiting for std.log) but
using write is a handy way for me to debug things in Scintilla.


More information about the Digitalmars-d-learn mailing list