🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #6080

Redirecting Console output

#6080
From: Robert L. Smith
Date: Tue, 07 Jun 2005 20:30
43 lines
2372 bytes
I am using a third-party DLL for which I don't have source code.  The DLL functions write their results to the console (STDOUT) as strings.  I would like to write a Win32 GUI program which would call the DLL functions and redirect the console output.  Most of the examples redirect output from a child process; I need to redirect console output from the main process.  So far, WriteFile is redirected but printf and cout are not.  Can anyone tell me what I'm doing wrong?  Here is some source code (error handling & variable declarations omitted):

//------------------------------------
    AllocConsole();
    // Create a pipe
    CreatePipe( &hPipeRead, &hPipeWrite, &SecurityAttributes,
        DEFAULT_BUFFER_SIZE);

    //  set handle to pipe to be STDOUT
    SetStdHandle( STD_OUTPUT_HANDLE, hPipeWrite);

    // this works
    char buf[] = "my dog has fleas\n";
    WriteFile(hPipeWrite, buf, sizeof(buf), &nWritten, NULL);

    // this doesn't work!
    printf("I am feeling so sad\n");

    hFile = CreateFile( "d:\\out.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL );

    CloseHandle(hPipeWrite);

    while (1)
    {
        if (!ReadFile(hPipeRead, Buf, sizeof(Buf), &dwRead, NULL))
            break;
        if (0 == dwRead) break;
        WriteFile(hFile, Buf, dwRead, &dwWritten, NULL);
    }

    SetStdHandle( STD_OUTPUT_HANDLE, hSTDOUT);
    CloseHandle(hFile);
    CloseHandle(hPipeRead);

    FreeConsole();

//----------end-----------

Thanks in advance,

Robert L. Smith
Technical Details
Message-ID: <42a603ef$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!meganewsservers.com!feeder2.on.meganewsservers.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,cfdddf60c0d5c3e9,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Robert L. Smith" <x@x>
Sender: "Robert L. Smith" <x@x>
Reply-To: "Robert L. Smith" <x@x>
Subject: Redirecting Console output
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 66.117.195.80     
NNTP-Posting-Host: 207.105.83.66
Message-ID: <42a603ef$1@newsgroups.borland.com>
Date: 7 Jun 2005 13:30:39 -0700
X-Trace: newsgroups.borland.com 1118176239 207.105.83.66 (7 Jun 2005 13:30:39 -0700)
Lines: 44
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!meganewsservers.com!feeder2.on.meganewsservers.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail