I'm not sure if this is possible - well, at least not using a simple simple API.
Please note that I did not find (although I tried) any information on the Internet, which directly confirms , but here is how I came to this conclusion.
The Windows console is its own subsystem managed by csrss.exe
(and also starting with Windows Vista conhost.exe
, but I'm distracted). It has its own APIs ( AttachConsole
, WriteConsole
, etc.), and you can only have one βconsoleβ for each process.
CMD.EXE
on the other hand, is just another console application that just uses the console and launches a console window. You can observe this effect by running another application in console mode and look at the process tree, for example. Process Explorer: the parent process CMD.EXE is missing (but rather, it is Explorer or everything that you used to start it, including, of course, CMD.EXE).
So far I have tried to show the difference between the "console" and CMD.EXE
, batch files or applications in console mode as a whole.
Therefore, when you use > CON
in CMD.EXE
, you actually cause the same effect as writing to CONOUT$
in your own applications (or your typical entry in /dev/console
on a UNIX-like OS). There seems to be no direct equivalent for managed code, since Console.Out
and Console.Error
are equal to stdout
and stderr
in native applications (or 1
and 2
as file descriptors in CMD.EXE
)).
With all that, when you start a process, you can redirect its standard output and standard error streams, but not (as such) messages that it writes to the console (or CONOUT$
descriptor). I assume that this will be the same as an attempt to redirect the output that the process writes to some file, which is also impossible (in general) possible.
You could achieve this using some kind of binding or injection of something inside the child process to capture the console output.
This cannot be done easily, is also (one of) the reason why replacing a full terminal (for example, a console window, not CMD.EXE
!) For Windows is not always possible and requires some hacks or workarounds.