[Haskell-cafe] How can I redirect stdout?

Krasimir Angelov kr.angelov at gmail.com
Mon Oct 2 03:55:46 EDT 2006

Hi Matthew,

On Windows stdout/stderr/stdin exists only when your application is
using the Console OS subsystem. All GUI applications doesn't have
console window and they don't have stdout/stderr/stdin. When you are
building DLLs then the subsystem is determined from the type of the
application that is loading your DLL. In your particular case the DLL
is loaded from Matlab which is definitely a GUI application. When you
are trying to load the DLL from other C application then probably it
is Console application and all print statements simply works. The
solution that you can use is to replace all your print statements with
Debug.Trace.trace or Debug.Trace.putTraceMsg. When you are using these
functions from Console application then the output is redirected to
stderr but when they are used from GUI application then the output
goes to the debugger. You can see the output in the Debug pane in the
Visual Studio IDE while you are running MatLab from the debugger.


On 9/29/06, Matthew Bromberg <mattcbro at earthlink.net> wrote:
> I'm reposting this in it's own new thread since I think it involves a
> general issue beyond exporting Haskell DLLs.
> I am having some problems with GHCs stdout when a Haskell program is
> called from a windows program.
> As I noted earlier I am calling some Haskell code from C as a bridge to
> being able to
> ultimately call Haskell from Matlab 6.5.
> The Haskell code is compiled into a .DLL file on a windows machine.
> Matlab calls some C code which then calls the Haskell code.
> As soon as it gets into the Haskell code I get this run time error in
> ghcDLL.dll
> <stdout>: hPutChars: invalid argument (Bad File Descriptor)
> The Haskell code seems to work correctly if called from a stand-alone C
> program. Moreover, if I scrub all print statements from the Haskell
> code, it appears to run correctly from Matlab.
> Matlab does not properly redirect stdout so any printf calls from the C
> code simply fail to print.
> However Haskell's behavior is to halt after generating a runtime error.
> The C code used to generate the Haskell .dll is of course mingw gcc, but
> the C code used to create my Matlab mex file is Microsoft VC++ 6.0.  I
> tried to redirect stdout using freopen() in C, but that never seems to
> work with Microsoft.  At any rate it certainly doesn't effect Haskells
> use of stdout.  I think in general for windows programs there is no
> stdout defined, whereas it's always defined for console programs.
> My question is, is there some way I can redirect stdout from inside
> Haskell so that all output is sent to a file?  Is there an equivalent of
> freopen() in Haskell that works?
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list