document updated 16 years ago, on May 6, 2008
APIs
Terminology
- console — Each process can only have one console. Each console can be shared by multiple processes. (AllocConsole/AttachConsole/FreeConsole)
- screen buffer — Multiple buffers that can be rapidly changed between, and modified in the background. (SetConsoleActiveScreenBuffer/CreateConsoleScreenBuffer)
- screen buffer window — A view of a subset of a screen buffer.
- alias — a native version of AutoHotkey's hotstrings
Distinctions
- There are several ways to build an application that is primarily console-based:
- compile as a Win32 console app, GetConsoleWindow(), and go nuts
- The primary benefit this gives you is that your app can be started within an existing cmd.exe (thus can have its stdin/stdout redirected), and it can be started from full-screen text-only mode as well.
- It can also access the Win32 API, but won't be able to run under 16-bit DOS.
- compile as a Win32 GUI app, AllocConsole(), and go nuts
- compile as a 16-bit DOS application
- downsides: can't call win32 GUI API; upsides: can run outside of Windows too
- modern versions of Visual Studio can't build 16-bit apps. Visual C++ v5.1 through v8.0 were able to build 16-bit apps.
- There are two modes that any Win32 console can be viewed in (alt-enter to toggle). It's important to be aware that there can be differences in rendering between the two.
- windowed
- implemented within csrss.exe (which runs with system privileges, so it's subject to heavy security considerations [1] [2] [3])
- full-screen text-only
- the hardware VGA card does the rendering (though Windows doesn't restore all undocumented settings when switching into windowed mode and then back [1] [2])
- not available from Vista onwards
HOWTO (both windowed and text-only modes)
- Mouse input:
(in text-only mode, there's a little square mouse that sends the exact same events as in GUI mode... remember edit.com?)
- SetConsoleMode(), +ENABLE_MOUSE_INPUT, –ENABLE_QUICK_EDIT_MODE (to modify quick-edit, you have to use an input buffer handle)
- ReadConsoleInput() to get the mouse events (.NET definitions: [1] [2])
(the event data isn't thoroughly documented. Some people have posted the results of their experimentation: [1])
- Change the palette:
- Double-buffering:
HOWTO (windowed mode only)
Internal details
- window class = ConsoleWindowClass, obviously
- winsrv.dll — loaded into csrss.exe
- ConsoleWindowProc(), handles console window messages
- for certain operations, it will create a new thread inside the console creator's process, and inject console.dll into that process (eg. make the Properties dialog appear)
- after the Properties dialog closes, to send the data back to csrss, it uses WM_USER+201
- console settings are saved in HKEY_CURRENT_USER\Console\, with sub-keys for "Apply to current window only" settings
- there are some new security restrictions in Vista, but cssrs.exe has a special exception
Speed/performance
Third-party .NET wrappers
Third-party C wrappers