Article View: borland.public.cppbuilder.nativeapi
Article #6081Re: Redirecting Console output
From: Remy Lebeau (TeamB)
Date: Tue, 07 Jun 2005 22:42
Date: Tue, 07 Jun 2005 22:42
50 lines
2631 bytes
2631 bytes
"Robert L. Smith" <x@x> wrote in message
news:42a603ef$1@newsgroups.borland.com...
> // this works
> char buf[] = "my dog has fleas\n";
> WriteFile(hPipeWrite, buf, sizeof(buf), &nWritten, NULL);
Of course it does, because you are writing to the pipe directly, not to
STDOUT.
> // this doesn't work!
> printf("I am feeling so sad\n");
Nor should it. SetStdHandle() has no effect on the handles that the C
functions use. The C library grabs the STDIN, STDOUT, and STDERR handles
once at program startup, stores them into an array, and uses the array for
all reading/writing operations. SetStdHandle() does not touch those array
items at all. The only offical way to redirect STDIN, STDOUT, and STDERR
from the C library perspective is to use freopen(), however it redirects to
files, not user-defined handles such as pipes. As far as I know, the only
way to specify your own handle is to use _open_osfhandle() and then reassign
STDOUT directly, for example (untested):
#include <io.h>
#include <stdio.h>
HANDLE hWritePipe = ...;
int iPipe = _open_osfhandle(reinterpret_cast<long>(hWritePipe),
_O_TEXT);
if( iPipe != -1 )
{
FILE *pFile = _fdopen(iPipe, "w");
if( pFile )
{
*stdout = *pFile;
// write to stdout as needed...
}
}
Once you have stdout redirected, you can optionally call setvbuf() if you
want to turn off the C library's native buffering, which will effectively
dump any written data to your pipe immediately on every C library writing
operation.
Also, if you want std::cout to be redirected as well, call the
ios::sync_with_stdio() function after redirectly STDOUT.
Gambit
Technical Details
Message-ID:
<42a622ac$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<42a603ef$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,cfdddf60c0d5c3e9 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <42a603ef$1@newsgroups.borland.com> Subject: Re: Redirecting Console output Date: Tue, 7 Jun 2005 15:42:24 -0700 Lines: 52 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <42a622ac$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1118184108 68.69.17.41 (7 Jun 2005 15:41:48 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mail