How to get stdout/stderr from unmanaged DLL?

G

Guest

I have setup Console redirection within my Console app (via Console.SetOut
and Console.SetErrror) to route console WriteLine calls to a logfile. This
works just fine with one exception...

I use PInvoke to call into several unmanaged DLLs which write diagnostic
information out to stdout (and possibly stderr). When I run in a
"standalone" mode with no redirection, these DLLs do generate output to the
console window. But I want to run in a "batch" mode where the app is
launched without a window. In this mode I still get all Console.WriteLine
and Console.Error.WriteLine output properly redirected, but lose all output
from the unmanaged DLLs.

Is there anything I can do to get output from an unmanaged, PInvoke'd DLL to
get redirected properly?

Thanks!
-- Tom
 
W

William Stacey [MVP]

Hi Thomas. What do you mean by "batch" mode without a window? Calling your
c# console app from a batch file?
 
G

Guest

Not necessarily, I just mean in a non-interactive way. Actually what I'm
trying to do is the following. I have a WindowsService which responds to
specific IPC commands by starting a utility application. It is this utility
application for which I want to get stdout redirected to a log file.

Reassigning my own TextWriter via Console.SetOut and Console.SetError *does*
correctly route all Console.WriteLine and Console.Error.WriteLine type of
calls from my managed code despite the fact that the app is running without a
console window. But any output from PInvoked calls to these unmanaged DLLs
is lost. I ensure that a Console exists for my application (via
AllocConsole) but this doesn't have any effect.

I know I could redirect to a temp file on the process startup from my
service and then get all output after the fact, but then I would lose the
sequence of output between console output from the managed code and unmanaged
DLL output.

-- Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top