Is it possible to intercept calls to console from another process?

727 Views Asked by At

The situation is that I have program started through system() or CreateProcess().

Now, is it possible to do stuff as that program outputs data into console. I mean as the program outputs it. That is not wait for the end, gather data and then process it, but just in the moment that this external program calls console with data that it wants to print, and then get hold of that data, process it and output something else on the console.

3

There are 3 best solutions below

0
On

On Linux, create a named pipe:

system("mkfifo pipename")

Then open the pipe in the first program, and start the program with:

system("program > pipename")

I'm not sure how to do this on Windows.

0
On

The easiest way is usually to start the program with _popen(your_program, "r");. That will return a FILE * you can read from, and what it reads will be whatever the child writes to its standard output. When you read EOF on that file, it means the child process has terminated. This makes it relatively easy to read and process the output from the child in real time.

0
On

Call AllocConsole before creating child process, or use AttachConsole(ChildPID) function (in parent process). After that, you may use any ReadConsoleXXX or WriteConsoleXXX functions.