473,747 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stdout redirection

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)GetStdHa ndle(STD_OUTPUT _HANDLE),_O_APP END);
fp = _fdopen(fd,"w") ;
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
But apparently it doesn’t work: ‘_open_osfhan dle’ 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
2 3606
"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:37******** *************** ***********@mic rosoft.com...
I am trying to redirect stdout to a RichEdit control, this is done by
...
fd = _open_osfhandle ((long)GetStdHa ndle(STD_OUTPUT _HANDLE),_O_APP END);
fp = _fdopen(fd,"w") ;
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
But apparently it doesn't work: '_open_osfhandl e' returns -1.


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

_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
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_osfhandl e' ????????

Following is the managed code used for redirecting to stdout:
m_StringWriter = new System.IO.Strin gWriter(m_Strin gBuilder);
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 applicationsrNa dav Rubinstein Nice Systems l.t.d., R&D Phone:
+972-9-775____ +972-52-2493760 e-mail:na******** ******@nice.com
so***@ddevel.co m
"William DePalo [MVP VC++]" <wi***********@ mvps.org> wrote in message
news:e9******** ******@TK2MSFTN GP14.phx.gbl...
"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:37******** *************** ***********@mic rosoft.com...
I am trying to redirect stdout to a RichEdit control, this is done by
...
fd = _open_osfhandle ((long)GetStdHa ndle(STD_OUTPUT _HANDLE),_O_APP END);
fp = _fdopen(fd,"w") ;
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
But apparently it doesn't work: '_open_osfhandl e' returns -1.
I resort to this redirection technique when I need to add more debug trace
capability than is convenient with OutputDebugStri ng().

_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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5638
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* . I have tried the simplest method of capturing stdout, stderr via: saveout = sys.stdout sys.stdout = file_obj
4
1927
by: Wes S. | last post by:
How can I redirect and tag sys.stdout values. I tried creating a class module that saved the old stdout and replaced it, but I can't seem to figure out how to do such. Here is what I got... class cStdOutRedirect : stdOld = "" def __main__(self) : stdOld = sys.stdout
0
1831
by: lickspittle | last post by:
Hi, I have Python embedded with my other code, and when my other code opens a console and redirects stdout, stdin and stderr to it, then calls PyRun_InteractiveLoop, it immediately returns with an EOF. After some debugging the reason for this appears to be that the stdin and stdout that the ReadLine function in the tokeniser include are not affected by my redirection and refer to a non-terminal so don't stall waiting for input from my...
10
21136
by: Michael Gaab | last post by:
If I redirect stdout by using freopen("afile", "w", stdout); and then I closed stdout using, fclose(stdout), essentially I am just closing "afile". I have to reestablish what stdout originally pointed to? thanks
0
3263
by: Nadav | last post by:
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:
2
5937
by: Thomas W. Brown | last post by:
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...
3
3104
by: mikem76 | last post by:
How do I automatically redirect stdout and stderr when using os.popen2 to start a long running process. If the process prints a lot of stuff to stdout it will eventually stop because it runs out of buffer space. Once I start reading the stdout file returned by os.popen2 then the process resumes. I know that I could just specify > /dev/null when starting the process but I'd like to know if there is a way to start a process using os.popen2...
1
3765
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a C# application in which I start another process which produces output to stdout and stderr. In fact, that process is the uSoft VS2005 C/C++ compiler itself! I would like to capture the results of the compile and display them in a RichTextBox. The problem I'm having is that when I intentionally introduce an error in the C code I'm compiling, I can't read the error output in my C# program. I've tried redirecting both...
0
1831
by: Tom Gaudasinski | last post by:
Greetings, I'm trying to redirect python's stdout to another location. The reason for this is that I'm embedding python in an application. Now, originally my code was developed for Linux and that did not require redirection due to the fact that every X11 application can have an STDOUT associated with it. I then proceeded to take interest in making my code portable and looked at compiling it on windows, however Win32 GUI applications are...
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9307
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9223
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8233
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4588
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.