0

Just like Visual Studio Code's integrated console, I would like to know how it is implemented.

I tried to read VSCode's source code, and I found that it is using xterm.js to provide terminal. Then I tried to read xterm.js's source code, but I didn't find any native(?) code about creating or integrating a terminal.

Maybe I missed something, please give me some advice if you can.

Is it a child process with its window SetParented to its parent?

Edit:

My goal is to embed a console window in my application, so that I can quickly run some scripts when using it, just like the experience in VS Code.

Sorry for the ambiguity in the original question.

Suen
  • 73
  • 6
  • You could do so with cmd.exe as child process. You might find some additional hints [here](https://stackoverflow.com/q/170800/1312382). If working with Qt, possibly [this](http://qconsole.sourceforge.net/) is interesting as well, maybe even the links sited [here](https://forum.qt.io/topic/6468/embedded-console-in-qt-gui/7). Did not evaluate any of, though... – Aconcagua Jul 08 '18 at 07:26
  • [Is it legal to have a cross-process parent/child or owner/owned window relationship?](https://blogs.msdn.microsoft.com/oldnewthing/20130412-00/?p=4683). While legal, you cannot safely do it, unless you own both processes, and know *exactly* what you are doing. At a guess, VSCode is opening pipes to a native console window, and just reads its output to its own window. – IInspectable Jul 08 '18 at 07:51
  • Is a console window something you want to implement in one of your own projects? If so, what do you actually want to _do_ in that window? If you state your requirements more clearly, you won't get downvoted and you'll get better answers (although the one below is certainly interesting). – Paul Sanders Jul 09 '18 at 08:00
  • @PaulSanders I've edit the description. I want to embed a console window in my application just like the one in VS Code. I don't mean to reinvent a console screen. Sorry for the ambiguity. – Suen Jul 11 '18 at 11:03

1 Answers1

2

VS Code creates named pipes for console input/output and then spawns the process winpty-agent.exe as well as powershell.exe. winpty-agent.exe opens the named pipes for CONIN$ and CONOUT$ and all input in the terminal window bound to VSCode either uses Read/WriteFile to pass information to and from pipes which then the information is passed to a hidden terminal window spawned by powershell.exe. The pipes are written to and read from each time a character is inputted.

winpty-agent.exe also makes the PowerShell window spawned from powershell.exe a child window and hides it with the flag SW_HIDE. When the window visibility state has the flag set SW_SHOW, it can be seen that every time something is inputted in the terminal bound to VSCode, is also inputted at the same time in the hidden PowerShell terminal window.

This article demonstrates how to redirect console input/output between parent and child processes via the utilization of pipes.

Jason
  • 150
  • 1
  • 10