Hiding the console on Windows

How to hide that annoying black console window that pops up behind your nice GUI program…

By default the console window is launched. It is possible to avoid this but if you do this you need to be careful to never use the standard input/output/error or your program will exit with an exception.

So that means never using putStr, print, getContents etc. If you do you’ll get an exception like "hPutChar(stdout): invalid file descriptior". This is because the standard input/output/error for Windows GUI programs is not connected and thus the standard file descriptors are not valid.

The way to hide the console window it is to pass a special linker option when compiling your program:

ghc --make Foo.hs -optl-mwindows

The reason we have not done this for the demos that come with Gtk2Hs on Windows is that many of them do write status messages to the console and so would fail if we did not show the console window.

If we could somehow improve this behaviour so it wasn’t so poor we could make the -optl-mwindows the default. But at the moment it would cause far too many questions about mysteriously failing programs. Better behaviour might be to create the console as soon as stdin/stdout/stderr is used but we would have to persuade the GHC developers to make such a change.

Comments are closed.