I am trying to write my own Command Line wrapper like ‘Console2’ in C#. Unfortunately I haven’t figured out a good way to do it.
Can somebody give me an example of grabbing console input and output and directing it into an Application?
NB I am not trying to make a console application, I am trying to make a wrapper for console – A windows forms application with a richtextedit which acts like a console and which can handle IO like Windows Console
You cannot reliably get all console output from a process by only redirecting its standard handles. As soon as it uses raw console I/O functions, these will only work with a real console and not file handles.
Normally, the default, unredirected
STD_INPUT_HANDLE,STD_OUTPUT_HANDLEandSTD_ERROR_HANDLEare only pseudo-handles, as in the sense of not being handles known to the NT kernel for that process. TheReadFileandWriteFileAPIs have a hack in them that checks for these pseudo-handles and redirects the call toReadConsoleAandWriteConsoleAas appropriate. However, all the console APIs only operate on console pseudo-handles (named console input buffers and console screen buffers) and will fail when passed a real file handle.Now, because of this redirection, and the fact that most programs use the file APIs when writing to or reading from the console means that it is possible to have some level of redirection, but since what you want to do is a complete console emulator, this will not be enough. You will not be able to capture any of the calls that, for example, change the size or attributes of the screen buffer, reads from it, creates alternate ones, etc.
If you are not scared of assembly language and reverse-engineering, you can look into hooking the various console APIs into the target process (and their children), or, in the case of Windows 7, reimplementing
conhost.exe.