By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

stdout redirection

P: n/a
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_HAND LE),_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.

--
Nadav
http://www.ddevel.com
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
I am trying to redirect stdout to a RichEdit control, this is done by
...
fd = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HAND LE),_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
Nov 17 '05 #2

P: n/a
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:na**************@nice.com
so***@ddevel.com
"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message
news:e9**************@TK2MSFTNGP14.phx.gbl...
"Nadav" <Na***@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
I am trying to redirect stdout to a RichEdit control, this is done by
...
fd = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HAND LE),_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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.