hsmyers has asked for the wisdom of the Perl Monks concerning the following question:

A test project leads me to ask how do I 'catch' STDOUT when embedding Perl56.dll (ActiveState) in a MFC/VC++ program? The embedding portion works fine---but I'd like better control over the environment, i.e. not misplace stdout and friends. Can anyone give me any pointers, links, advice? I've tried a number of re-direction traps using _dup and _dup2, so far with no luck. What I'd really like is to redirect to a buffer for display and manipulation.

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Replies are listed 'Best First'.
Re: Perl56.dll and STDOUT
by PodMaster (Abbot) on May 02, 2003 at 16:46 UTC
    Check perlio, perliol, perlclib, perlapi, perlembed perlcall

    I think you might be able to (from c) use one of these

    stdin PerlIO_stdin() stdout PerlIO_stdout() stderr PerlIO_stderr()
    The easiest would be to call a perl function with a filename as an argument, and have it setup a filehandle (global symbol).

    tye says: it's easier to do it in perl ;)

    update: ask on the p5p mailing list (http://lists.perl.org).


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      PerlIO_std... looks interesting. If I can retrieve it then I can muck about with it to my hearts content---actually all I want to do is display it in a 'nearly' realtime text window...Whole thing started as a challenge to drop 'perl' into a text editor and use it as a macro language<g>! Fun so far.

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: Perl56.dll and STDOUT
by hardburn (Abbot) on May 02, 2003 at 16:12 UTC

    Could you close the current STDOUT and then reopen it as a pipe into your program?

    Never tried this before, but that's the first solution that pops to mind.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Perl56.dll and STDOUT
by Anonymous Monk on May 02, 2003 at 20:26 UTC
    close STDOUT;
    open STDOUT, ">stdout.log";

    works for mortal applications. Your embeded system could use:

    open STDOUT, ">&4"; # i think
    or
    mkfifo              # if your system supports it. M$, not.
    You can also reopen your own stdout in the C code portion. Then attach that file handle to your tty screen buffer.
      That would work if I could rewrite the perl buffer---but in a sense that would be cheating. I've found that the following code seems to do the trick:
      stream = freopen("data.txt","w+",PerlIO_stdout()); ASSERT(stream); setbuf(stream,mybuffer); memset(mybuffer,0,BUFSIZ);
      This gives me my own stdout buffer with which I can have my way! BTW this is not a console app, so there is no stdout, hence the use of PerlIO_stdout(). (thanks to podmaster and tye for that pointer...) So now I'll have one window for code, one for stdout, and one for stderr---or whatever! Joy!

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."