stdout redirection

G

Guest

Hi,

Introduction:
***************
I am trying to redirect stdout to a RichEdit control, this is done by
initiating a StringWriter, associated it with a StringBuilder and setting the
Console.Out to the String Writer, when ever the RichEdit is to be updated
text from the StringBuilder is being copied.

The Problem:
***************
What was just described works well as long as I am dealing with managed
code, my managed app loads some inproc unmanaged DLLs using PInvoke, these
DLLs are using printf*.* to write to the stdout, BUT when redirecting stdout
in the manner just described all of the data being printed by the unmanaged
app doesn’t show on the redirected console output.
I have tried redirecting the unmanaged code output in the following manner:
int fd = 0;
FILE *fp = 0;
fd = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE),_O_APPEND);
fp = _fdopen(fd,"w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
But apparently it doesn’t work: ‘_open_osfhandle’ returns -1. ( internally
GetFileType doesn’t recognize the stdout handle returned by ‘GetStdHandle’ As
a valid file handle…

What should I do how can I redirect the unmanaged stdout to the same
destination as the managed stdout? I would expect the unmanaged code to
transparently write to the redirect stdout….

Any help would be appreciated.
 
W

William DePalo [MVP VC++]

Nadav said:
I am trying to redirect stdout to a RichEdit control, this is done by
...
fd = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE),_O_APPEND);
fp = _fdopen(fd,"w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
But apparently it doesn't work: '_open_osfhandle' returns -1.

I resort to this redirection technique when I need to add more debug trace
capability than is convenient with OutputDebugString().

_If_ I recall correctly, when you redirect the standard output device of a
console application, any output you do in the main module _or_ in a DLL gets
redirected properly.

However, if you AllocConsole() in a windowed application and redirect
standard output to it, then the redirection works _only_ in the main module.
Any DLL loaded by the executable still "thinks" that it does not have a
standard output device because the executable image was not marked as a
console application when the DLL's runtime was initialized.

So, my _guess_ is that you need to pass the device handle of the console to
whatever executable component is redirecting output and redirect its
standard output as well. Of course, I might be wrong.

Regards,
Will
 
N

Nadav

And how can I get a handle to a managed stream associated with the console?
How can I get the handle to the redirected stream of a managed StringWriter?
Following is the redirection done through C#, I need the stdout of the
associated unmanaged DLLs to be routed to the StringWriter, how do I do
that? how do I get the unmanaged stream handle of the managed StringWriter?
what should I pass to '_open_osfhandle' ????????

Following is the managed code used for redirecting to stdout:
m_StringWriter = new System.IO.StringWriter(m_StringBuilder);
Console.SetOut(m_StringWriter);

Nadav
http://www.ddevel.com

Homepage:http://www.ddevel.com ASTAutoStack Tracer Enhance windows 'common
error' reporting mechanism DDrCOM Monitor Debug Monitor and Reverse engineer
COM based applicationsrNadav Rubinstein Nice Systems l.t.d., R&D Phone:
+972-9-775____ +972-52-2493760 e-mail:[email protected]
(e-mail address removed)
 

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