[Haskell-cafe] GHC throws IOError on Win32 when there is no console

Paul Moore p.f.moore at gmail.com
Fri Feb 9 18:00:56 EST 2007


On 09/02/07, John Ky <newhoggy at gmail.com> wrote:
> I noticed on Windows that when I use IO functions that write to stdout when
> the process is lacking a console, those functions throw an IOError.  I'm not
> sure if this also occurs for stderr because I haven't tried it.

This is Windows standard behaviour. For GUI applications, the handles
corresponding to std{in,out,err} are not valid, and attempts to write
to them give an error.

In general, you shouldn't use these handles in a GUI program. Or if
you can't guarantee that the libraries you use won't write to them,
redirect them to the null device or a log file.

(I'm not saying this is ideal behaviour, but that's how it is...)

> I believe the most sensible behaviour should be for those functions to not
> throw, but instead do nothing.

That's not 100% clear - writing to a log file may be better (I don't
like the idea of dropping potentially useful information without any
warning - and if the output wasn't useful to *someone*, why was it
being written?)

Annoyingly, Haskell doesn't seem to provide a way to reopen handles
(specifically, the standard ones). You may have to use the FFI and the
Windows SetStdHandle API to reset the handles.

It probably wouldn't be hard to write a reasonably general wrapper for
this, but it's a bit late now so I'll leave that as an exercise :-)

Paul.

Paul.


More information about the Haskell-Cafe mailing list