views:

1318

answers:

4

I want to execute a certain batch file and redirect its console output to a text control in visual c++ or redirect the console output at the same time the logs/echo are showing.

+4  A: 

Basically, you have to make the run process to write to a pipe, and to read the output of this pipe.

[EDIT] I know how SciTE does that (you can take a look at the source: win32/SciTEWin.cxx, ExecuteOne function), I searched a slightly more generic way, found How to spawn console processes with redirected standard handles from Microsoft itself.
If you seach CreatePipe PeekNamedPipe CreateProcess keyword, for example, you might find other examples.

PhiLho
A: 

There's an article here on CodeProject that shows how to do this.

ChrisN
A: 

If elegance is not a priority then a really simple solution might be to redirect the output to a file, and then read in the file contents.

David Sykes
+1  A: 

Another option is to use Boost.Process (Boost.Process is not (yet) an official Boost C++ library. It must be downloaded and installed separately).

The example "Child.4 - Reading from a child using asynchronous I/O" shows how to redirect the output of the child process into a C++ stream (and later access the content).

Example "Child.4 - Reading from a child using asynchronous I/O" show how to use Boost.Process togehter with Boost.Asio to access the childs I/O asynchronous.

The advantages of this method is, that Boost.Process supports the Windows API and POSIX API.

jk