How to read output written to the console

Posted on 2006-03-22
Last Modified: 2011-10-03
Hi Experts,

I have a an executable file( info.exe) that I call within my program to get some information.  I used CreateProcess to execute this .exe within my program.  When executed from a command prompt, the .exe returns some data on the console.  I want my program to read that output from the .exe.   How can I do that?

Question by:thanesh
    LVL 20

    Expert Comment

    hi thanesh,

    you can always let the application output write to a file instead to the console. if you execute a program like this

    "program.exe > c:\your_output_file"

    the output is written into that file. then you can read it ...

    hope it helps,
    LVL 20

    Expert Comment

    you can even split it into stdout and strerr - output.

    write stdout to file:
    "program.exe 1> c:\your_output_file"

    write stderr to file:
    "program.exe 2> c:\your_output_file"

    LVL 2

    Accepted Solution

    Hi thanesh,
                     You need some kind of communication channel (usually one more .exe that can transmit information)between the two programs to make those two talk to one another. You cannot directly read output from a program.

    The information from one exe should be packaged and sent out which is received at the other end in the same form. If you want to do this in .NET you can use the concept of remoting, in C++ I remember something called RTI(runtime infrastructure) that would take care of this communication process.

    hope this helps,
    LVL 2

    Expert Comment

    hi thanesh

    You can consider executing the process with _popen() instead of CreateProcess()

    LVL 5

    Expert Comment


    You can do the following:

    - create an anonymous pipe (CreatePipe). Say the handle is then hPipe
    - now create a STARTUPINFO structure and set the standard output handle = hPipe
    - now create your process (CreateProcess) and as one of the params pass the startupinfo struct that you have created above
    - the process will run and output will go to the pipe
    - you can now read that output from the pipe use ReadFile until there is no more data left.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now