On Windows, how does console window ownership work?

前端 未结 6 711
無奈伤痛
無奈伤痛 2021-01-13 01:05

When a console application is started from another console application, how does console ownership work?

I see four possibilities:

  1. The second applicati
6条回答
  •  隐瞒了意图╮
    2021-01-13 01:59

    The way the SDK talks about it strongly resembles 1. It is an option with CreateProcess, described as follows:

    CREATE_NEW_CONSOLE
    The new process has a new console, instead of inheriting its parent's console (the default). For more information, see Creation of a Console.

    Output however happens through handles, you'd get one with GetStdHandle(). Passing STD_OUTPUT_HANDLE returns the console handle, assuming output isn't redirected. Actual output is done through WriteFile() or WriteConsole/Output(). If both processes keep writing output to the handle then their output will be randomly intermingled. This is otherwise indistinguishable from what would happen when two programs write to the same file handle.

    Logically, there's a screen buffer associated with a console. You can tinker with it with SetConsoleScreenBufferXxx(). From that point of view you could call it shared memory. The actual implementation is undiscoverable, handles abstract them away, like any Win32 API. It is sure to have changed considerably in Vista with the new conhost.exe process.

提交回复
热议问题