using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Vanara.Extensions;
using Vanara.InteropServices;
namespace Vanara.PInvoke
{
public static partial class Kernel32
{
/// Use the console of the parent of the current process.
public const uint ATTACH_PARENT_PROCESS = unchecked((uint)-1);
///
/// An application-defined function used with the SetConsoleCtrlHandler function. A console process uses this function to handle
/// control signals received by the process. When the signal is received, the system creates a new thread in the process to execute
/// the function.
///
/// The type of control signal received by the handler.
///
/// If the function handles the control signal, it should return TRUE (1). If it returns FALSE (0), the next handler function in the
/// list of handlers for this process is used.
///
[return: MarshalAs(UnmanagedType.Bool)]
public delegate bool HandlerRoutine(CTRL_EVENT CtrlType);
///
/// Character attributes can be divided into two classes: color and DBCS. The following attributes are defined in the Wincon.h
/// header file.
///
[PInvokeData("wincon.h")]
[Flags]
public enum CHARACTER_ATTRIBUTE : ushort
{
/// Text color contains blue.
FOREGROUND_BLUE = 1,
/// Text color contains green.
FOREGROUND_GREEN = 2,
/// Text color contains red.
FOREGROUND_RED = 4,
/// Text color is intensified.
FOREGROUND_INTENSITY = 8,
/// Background color contains blue.
BACKGROUND_BLUE = 16,
/// Background color contains green.
BACKGROUND_GREEN = 32,
/// Background color contains red.
BACKGROUND_RED = 64,
/// Background color is intensified.
BACKGROUND_INTENSITY = 128,
/// Leading byte.
COMMON_LVB_LEADING_BYTE = 256,
/// Trailing byte.
COMMON_LVB_TRAILING_BYTE = 512,
/// Top horizontal.
COMMON_LVB_GRID_HORIZONTAL = 1024,
/// Left vertical.
COMMON_LVB_GRID_LVERTICAL = 2048,
/// Right vertical.
COMMON_LVB_GRID_RVERTICAL = 4096,
/// Reverse foreground and background attributes.
COMMON_LVB_REVERSE_VIDEO = 16384,
/// Underscore.
COMMON_LVB_UNDERSCORE = 32768,
}
/// Used by and .
[PInvokeData("ConsoleApi.h", MSDNShortId = "49adf618-196d-4490-93ca-cd177807f58e")]
[Flags]
public enum CONSOLE_INPUT_MODE : uint
{
///
NONE = 0U,
///
/// CTRL+C is processed by the system and is not placed in the input buffer. If the input buffer is being read by ReadFile or
/// ReadConsole, other control keys are processed by the system and are not returned in the ReadFile or ReadConsole buffer. If
/// the ENABLE_LINE_INPUT mode is also enabled, backspace, carriage return, and line feed characters are handled by the system.
///
ENABLE_PROCESSED_INPUT = 0x0001,
///
/// The ReadFile or ReadConsole function returns only when a carriage return character is read. If this mode is disabled, the
/// functions return when one or more characters are available.
///
ENABLE_LINE_INPUT = 0x0002,
///
/// Characters read by the ReadFile or ReadConsole function are written to the active screen buffer as they are read. This mode
/// can be used only if the ENABLE_LINE_INPUT mode is also enabled.
///
ENABLE_ECHO_INPUT = 0x0004,
///
/// User interactions that change the size of the console screen buffer are reported in the console's input buffer. Information
/// about these events can be read from the input buffer by applications using the ReadConsoleInput function, but not by those
/// using ReadFile or ReadConsole.
///
ENABLE_WINDOW_INPUT = 0x0008,
///
/// If the mouse pointer is within the borders of the console window and the window has the keyboard focus, mouse events
/// generated by mouse movement and button presses are placed in the input buffer. These events are discarded by ReadFile or
/// ReadConsole, even when this mode is enabled.
///
ENABLE_MOUSE_INPUT = 0x0010,
///
/// When enabled, text entered in a console window will be inserted at the current cursor location and all text following that
/// location will not be overwritten. When disabled, all following text will be overwritten.
///
ENABLE_INSERT_MODE = 0x0020,
///
/// This flag enables the user to use the mouse to select and edit text. To enable this mode, use . To disable this mode, use
/// ENABLE_EXTENDED_FLAGS without this flag.
///
ENABLE_QUICK_EDIT_MODE = 0x0040,
/// Use with ENABLE_QUICK_EDIT_MODE to enable or disable that flag.
ENABLE_EXTENDED_FLAGS = 0x0080,
/// Undocumented. Causes error in SetConsoleMode if used.
ENABLE_AUTO_POSITION = 0x0100,
///
/// Setting this flag directs the Virtual Terminal processing engine to convert user input received by the console window into
/// Console Virtual Terminal Sequences that can be retrieved by a supporting application through WriteFile or WriteConsole
/// functions. The typical usage of this flag is intended in conjunction with ENABLE_VIRTUAL_TERMINAL_PROCESSING on the output
/// handle to connect to an application that communicates exclusively via virtual terminal sequences.
///
ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200,
}
/// Used by and .
[PInvokeData("ConsoleApi.h", MSDNShortId = "49adf618-196d-4490-93ca-cd177807f58e")]
[Flags]
public enum CONSOLE_OUTPUT_MODE : uint
{
///
/// Characters written by the WriteFile or WriteConsole function or echoed by the ReadFile or ReadConsole function are examined
/// for ASCII control sequences and the correct action is performed. Backspace, tab, bell, carriage return, and line feed
/// characters are processed.
///
ENABLE_PROCESSED_OUTPUT = 0x0001,
///
/// When writing with WriteFile or WriteConsole or echoing with ReadFile or ReadConsole, the cursor moves to the beginning of
/// the next row when it reaches the end of the current row. This causes the rows displayed in the console window to scroll up
/// automatically when the cursor advances beyond the last row in the window. It also causes the contents of the console screen
/// buffer to scroll up (discarding the top row of the console screen buffer) when the cursor advances beyond the last row in
/// the console screen buffer. If this mode is disabled, the last character in the row is overwritten with any subsequent characters.
///
ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002,
///
/// When writing with WriteFile or WriteConsole, characters are parsed for VT100 and similar control character sequences that
/// control cursor movement, color/font mode, and other operations that can also be performed via the existing Console APIs. For
/// more information, see Console Virtual Terminal Sequences.
///
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004,
///
/// When writing with WriteFile or WriteConsole, this adds an additional state to end-of-line wrapping that can delay the cursor
/// move and buffer scroll operations.
///
/// Normally when ENABLE_WRAP_AT_EOL_OUTPUT is set and text reaches the end of the line, the cursor will immediately move to the
/// next line and the contents of the buffer will scroll up by one line. In contrast with this flag set, the scroll operation
/// and cursor move is delayed until the next character arrives. The written character will be printed in the final position on
/// the line and the cursor will remain above this character as if ENABLE_WRAP_AT_EOL_OUTPUT was off, but the next printable
/// character will be printed as if ENABLE_WRAP_AT_EOL_OUTPUT is on. No overwrite will occur. Specifically, the cursor quickly
/// advances down to the following line, a scroll is performed if necessary, the character is printed, and the cursor advances
/// one more position.
///
///
/// The typical usage of this flag is intended in conjunction with setting ENABLE_VIRTUAL_TERMINAL_PROCESSING to better emulate
/// a terminal emulator where writing the final character on the screen (in the bottom right corner) without triggering an
/// immediate scroll is the desired behavior.
///
///
DISABLE_NEWLINE_AUTO_RETURN = 0x0008,
///
/// The APIs for writing character attributes including WriteConsoleOutput and WriteConsoleOutputAttribute allow the usage of
/// flags from character attributes to adjust the color of the foreground and background of text. Additionally, a range of DBCS
/// flags was specified with the COMMON_LVB prefix. Historically, these flags only functioned in DBCS code pages for Chinese,
/// Japanese, and Korean languages. With exception of the leading byte and trailing byte flags, the remaining flags describing
/// line drawing and reverse video (swap foreground and background colors) can be useful for other languages to emphasize
/// portions of output.
///
/// With exception of the leading byte and trailing byte flags, the remaining flags describing line drawing and reverse video
/// (swap foreground and background colors) can be useful for other languages to emphasize portions of output.
///
/// Setting this console mode flag will allow these attributes to be used in every code page on every language.
///
/// It is off by default to maintain compatibility with known applications that have historically taken advantage of the console
/// ignoring these flags on non-CJK machines to store bits in these fields for their own purposes or by accident.
///
///
/// Note that using the ENABLE_VIRTUAL_TERMINAL_PROCESSING mode can result in LVB grid and reverse video flags being set while
/// this flag is still off if the attached application requests underlining or inverse video via Console Virtual Terminal Sequences.
///
///
ENABLE_LVB_GRID_WORLDWIDE = 0x0010,
}
/// The console selection indicator for .
[PInvokeData("ConsoleApi3.h")]
[Flags]
public enum CONSOLE_SELECTION
{
/// Mouse is down
CONSOLE_MOUSE_DOWN = 0x0008,
/// Selecting with the mouse
CONSOLE_MOUSE_SELECTION = 0x0004,
/// No selection
CONSOLE_NO_SELECTION = 0x0000,
/// Selection has begun
CONSOLE_SELECTION_IN_PROGRESS = 0x0001,
/// Selection rectangle is not empty
CONSOLE_SELECTION_NOT_EMPTY = 0x0002,
}
/// The type of console screen buffer to create.
[PInvokeData("Wincon.h")]
public enum CONSOLE_TEXTMODE
{
/// The console textmode buffer.
CONSOLE_TEXTMODE_BUFFER = 1
}
/// The state of the control keys.
[PInvokeData("Wincon.h")]
[Flags]
public enum CONTROL_KEY_STATE
{
///
NONE = 0,
/// The CAPS LOCK light is on.
CAPSLOCK_ON = 0x0080,
/// The key is enhanced.
ENHANCED_KEY = 0x0100,
/// The left ALT key is pressed.
LEFT_ALT_PRESSED = 0x0002,
/// The left CTRL key is pressed.
LEFT_CTRL_PRESSED = 0x0008,
/// The NUM LOCK light is on.
NUMLOCK_ON = 0x0020,
/// The right ALT key is pressed.
RIGHT_ALT_PRESSED = 0x0001,
/// The right CTRL key is pressed.
RIGHT_CTRL_PRESSED = 0x0004,
/// The SCROLL LOCK light is on.
SCROLLLOCK_ON = 0x0040,
/// The SHIFT key is pressed.
SHIFT_PRESSED = 0x0010,
}
/// The type of signal to be generated.
[PInvokeData("Wincon.h")]
public enum CTRL_EVENT
{
///
/// Generates a CTRL+C signal. This signal cannot be generated for process groups. If dwProcessGroupId is nonzero, this function
/// will succeed, but the CTRL+C signal will not be received by processes within the specified process group.
///
CTRL_C_EVENT = 0,
/// Generates a CTRL+BREAK signal.
CTRL_BREAK_EVENT = 1,
///
/// A signal that the system sends to all processes attached to a console when the user closes the console (either by clicking
/// Close on the console window's window menu, or by clicking the End Task button command from Task Manager).
///
CTRL_CLOSE_EVENT = 2,
///
/// A signal that the system sends to all console processes when a user is logging off. This signal does not indicate which user
/// is logging off, so no assumptions can be made.
///
/// Note that this signal is received only by services. Interactive applications are terminated at logoff, so they are not
/// present when the system sends this signal.
///
///
CTRL_LOGOFF_EVENT = 5,
///
/// A signal that the system sends when the system is shutting down. Interactive applications are not present by the time the
/// system sends this signal, therefore it can be received only be services in this situation. Services also have their own
/// notification mechanism for shutdown events. For more information, see Handler.
/// This signal can also be generated by an application using GenerateConsoleCtrlEvent.
///
CTRL_SHUTDOWN_EVENT = 6,
}
/// The type of input event.
[PInvokeData("Wincon.h")]
[Flags]
public enum EVENT_TYPE : ushort
{
/// The Event member contains a KEY_EVENT_RECORD structure with information about a keyboard event.
KEY_EVENT = 0x0001,
///
/// The Event member contains a MOUSE_EVENT_RECORD structure with information about a mouse movement or button press event.
///
MOUSE_EVENT = 0x0002,
///
/// The Event member contains a WINDOW_BUFFER_SIZE_RECORD structure with information about the new size of the console screen buffer.
///
WINDOW_BUFFER_SIZE_EVENT = 0x0004,
/// The Event member contains a MENU_EVENT_RECORD structure. These events are used internally and should be ignored.
MENU_EVENT = 0x0008,
/// The Event member contains a FOCUS_EVENT_RECORD structure.
FOCUS_EVENT = 0x0010
}
/// The display mode of the console.
[PInvokeData("Wincon.h")]
public enum GET_CONSOLE_DISPLAY_MODE
{
///
/// Full-screen console. The console is in this mode as soon as the window is maximized. At this point, the transition to
/// full-screen mode can still fail.
///
CONSOLE_FULLSCREEN = 1,
///
/// Full-screen console communicating directly with the video hardware. This mode is set after the console is in
/// CONSOLE_FULLSCREEN mode to indicate that the transition to full-screen mode has completed.
///
CONSOLE_FULLSCREEN_HARDWARE = 2
}
/// The status of the mouse buttons.
[PInvokeData("Wincon.h")]
[Flags]
public enum MOUSE_BUTTON_STATE
{
///
NONE = 0,
/// The leftmost mouse button.
FROM_LEFT_1ST_BUTTON_PRESSED = 1,
/// The second button from the left.
RIGHTMOST_BUTTON_PRESSED = 2,
/// The third button from the left.
FROM_LEFT_2ND_BUTTON_PRESSED = 4,
/// The fourth button from the left.
FROM_LEFT_3RD_BUTTON_PRESSED = 8,
/// The rightmost mouse button.
FROM_LEFT_4TH_BUTTON_PRESSED = 16,
}
/// The type of mouse event.
[PInvokeData("Wincon.h")]
[Flags]
public enum MOUSE_EVENT_FLAG
{
///
NONE = 0,
///
/// The second click (button press) of a double-click occurred. The first click is returned as a regular button-press event.
///
DOUBLE_CLICK = 0x0002,
///
/// The horizontal mouse wheel was moved. If the high word of the dwButtonState member contains a positive value, the wheel was
/// rotated to the right. Otherwise, the wheel was rotated to the left.
///
MOUSE_HWHEELED = 0x0008,
/// A change in mouse position occurred.
MOUSE_MOVED = 0x0001,
///
/// The vertical mouse wheel was moved. If the high word of the dwButtonState member contains a positive value, the wheel was
/// rotated forward, away from the user. Otherwise, the wheel was rotated backward, toward the user.
///
MOUSE_WHEELED = 0x0004,
}
/// Flags for .
[PInvokeData("ConsoleApi.h")]
public enum PSEUDOCONSOLE
{
/// Perform a standard pseudoconsole creation.
PSEUDOCONSOLE_STANDARD = 0,
/// The created pseudoconsole session will attempt to inherit the cursor position of the parent console.
PSEUDOCONSOLE_INHERIT_CURSOR = 1,
}
/// The display mode of the console.
[PInvokeData("Wincon.h")]
public enum SET_CONSOLE_DISPLAY_MODE
{
/// Text is displayed in full-screen mode.
CONSOLE_FULLSCREEN_MODE = 1,
/// Text is displayed in a console window.
CONSOLE_WINDOWED_MODE = 2,
}
/// Defines a console alias for the specified executable.
/// The console alias to be mapped to the text specified by Target.
/// The text to be substituted for Source. If this parameter is NULL, then the console alias is removed.
/// The name of the executable file for which the console alias is to be defined.
///
/// If the function succeeds, the return value is TRUE.
/// If the function fails, the return value is FALSE. To get extended error information, call GetLastError.
///
// BOOL WINAPI AddConsoleAlias( _In_ LPCTSTR Source, _In_ LPCTSTR Target, _In_ LPCTSTR ExeName );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AddConsoleAlias(string Source, string Target, string ExeName);
/// Allocates a new console for the calling process.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI AllocConsole(void);
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AllocConsole();
/// Attaches the calling process to the console of the specified process.
///
/// The identifier of the process whose console is to be used. This parameter can be one of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// pid
/// Use the console of the specified process.
///
/// -
/// ATTACH_PARENT_PROCESS (DWORD)-1
/// Use the console of the parent of the current process.
///
///
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI AttachConsole( _In_ DWORD dwProcessId );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AttachConsole(uint dwProcessId);
/// Closes a pseudoconsole from the given handle.
/// [in] A handle to an active psuedoconsole as opened by CreatePseudoConsole.
/// none
///
/// Upon closing a pseudoconsole, client applications attached to the session will be terminated as well.
/// A final painted frame may arrive on
/// hOutput
/// from the pseudoconsole when this API is called. It is expected that the caller will drain this information from the
/// communication channel buffer and either present it or discard it. Failure to drain the buffer may cause the Close call to wait
/// indefinitely until it is drained or the communication channels are broken another way.
///
///
// https://docs.microsoft.com/en-us/windows/console/closepseudoconsole void WINAPI ClosePseudoConsole( _In_ HPCON hPC );
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ConsoleApi.h")]
public static extern void ClosePseudoConsole([In] HPCON hPC);
/// Creates a console screen buffer.
///
/// The access to the console screen buffer. For a list of access rights, see Console Buffer Security and Access Rights.
///
///
/// This parameter can be zero, indicating that the buffer cannot be shared, or it can be one or more of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// FILE_SHARE_READ 0x00000001
/// Other open operations can be performed on the console screen buffer for read access.
///
/// -
/// FILE_SHARE_WRITE 0x00000002
/// Other open operations can be performed on the console screen buffer for write access.
///
///
///
///
///
/// A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child
/// processes. If lpSecurityAttributes is NULL, the handle cannot be inherited. The lpSecurityDescriptor member of the
/// structure specifies a security descriptor for the new console screen buffer. If lpSecurityAttributes is NULL, the console
/// screen buffer gets a default security descriptor. The ACLs in the default security descriptor for a console screen buffer come
/// from the primary or impersonation token of the creator.
///
/// The type of console screen buffer to create. The only supported screen buffer type is CONSOLE_TEXTMODE_BUFFER.
/// Reserved; should be NULL.
///
/// If the function succeeds, the return value is a handle to the new console screen buffer.
/// If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
///
// HANDLE WINAPI CreateConsoleScreenBuffer( _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode, _In_opt_ const SECURITY_ATTRIBUTES
// *lpSecurityAttributes, _In_ DWORD dwFlags, _Reserved_ LPVOID lpScreenBufferData );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern SafeHFILE CreateConsoleScreenBuffer(ACCESS_MASK dwDesiredAccess, FileShare dwShareMode,
SECURITY_ATTRIBUTES lpSecurityAttributes = null, CONSOLE_TEXTMODE dwFlags = CONSOLE_TEXTMODE.CONSOLE_TEXTMODE_BUFFER, IntPtr lpScreenBufferData = default);
/// Creates a new pseudoconsole object for the calling process.
///
/// [in] The dimensions of the window/buffer in count of characters that will be used on initial creation of the pseudoconsole. This
/// can be adjusted later with ResizePseudoConsole.
///
///
/// [in] An open handle to a stream of data that represents user input to the device. This is currently restricted to synchronous I/O.
///
///
/// [in] An open handle to a stream of data that represents application output from the device. This is currently restricted to
/// synchronous I/O.
///
///
/// [in] The value can be one of the following:
///
///
/// Value
/// Meaning
///
/// -
/// 0
/// Perform a standard pseudoconsole creation.
///
/// -
/// PSEUDOCONSOLE_INHERIT_CURSOR (DWORD)1
/// The created pseudoconsole session will attempt to inherit the cursor position of the parent console.
///
///
///
/// [out] Pointer to a location that will receive a handle to the new pseudoconsole device.
///
/// Type: HRESULT
/// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
///
///
///
/// This function is primarily used by applications attempting to be a terminal window for a command-line user interface (CUI)
/// application. The callers become responsible for presentation of the information on the output stream and for collecting user
/// input and serializing it into the input stream.
///
/// The input and output streams encoded as UTF-8 contain plain text interleaved with Virtual Terminal Sequences.
///
/// On the output stream, the virtual terminal sequences can be decoded by the calling application to layout and present the plain
/// text in a display window.
///
///
/// On the input stream, plain text represents standard keyboard keys input by a user. More complicated operations are represented
/// by encoding control keys and mouse movements as virtual terminal sequences embedded in this stream.
///
/// The handle created by this function must be closed with ClosePseudoConsole when operations are complete.
/// If using
/// PSEUDOCONSOLE_INHERIT_CURSOR
/// , the calling application should be prepared to respond to the request for the cursor state in an asynchronous fashion on a
/// background thread by forwarding or interpreting the request for cursor information that will be received on
/// hOutput
/// and replying on
/// hInput
/// . Failure to do so may cause the calling application to hang while making another request of the pseudoconsole system.
///
///
// https://docs.microsoft.com/en-us/windows/console/createpseudoconsole HRESULT WINAPI CreatePseudoConsole( _In_ COORD size, _In_
// HANDLE hInput, _In_ HANDLE hOutput, _In_ DWORD dwFlags, _Out_ HPCON* phPC );
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ConsoleApi.h")]
public static extern HRESULT CreatePseudoConsole([In] COORD size, [In] HFILE hInput, [In] HFILE hOutput, [In] PSEUDOCONSOLE dwFlags, out SafeHPCON phPC);
///
/// Sets the character attributes for a specified number of character cells, beginning at the specified coordinates in a screen buffer.
///
///
/// A handle to the console screen buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// The attributes to use when writing to the console screen buffer. For more information, see Character Attributes.
///
/// The number of character cells to be set to the specified color attributes.
///
/// A COORD structure that specifies the character coordinates of the first cell whose attributes are to be set.
///
///
/// A pointer to a variable that receives the number of character cells whose attributes were actually set.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI FillConsoleOutputAttribute( _In_ HANDLE hConsoleOutput, _In_ WORD wAttribute, _In_ DWORD nLength, _In_ COORD
// dwWriteCoord, _Out_ LPDWORD lpNumberOfAttrsWritten );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FillConsoleOutputAttribute(HFILE hConsoleOutput, CHARACTER_ATTRIBUTE wAttribute, uint nLength, COORD dwWriteCoord, out uint lpNumberOfAttrsWritten);
/// Writes a character to the console screen buffer a specified number of times, beginning at the specified coordinates.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
/// The character to be written to the console screen buffer.
/// The number of character cells to which the character should be written.
///
/// A COORD structure that specifies the character coordinates of the first cell to which the character is to be written.
///
///
/// A pointer to a variable that receives the number of characters actually written to the console screen buffer.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI FillConsoleOutputCharacter( _In_ HANDLE hConsoleOutput, _In_ TCHAR cCharacter, _In_ DWORD nLength, _In_ COORD
// dwWriteCoord, _Out_ LPDWORD lpNumberOfCharsWritten );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FillConsoleOutputCharacter(HFILE hConsoleOutput, char cCharacter, uint nLength, COORD dwWriteCoord, out uint lpNumberOfCharsWritten);
/// Flushes the console input buffer. All input records currently in the input buffer are discarded.
///
/// A handle to the console input buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI FlushConsoleInputBuffer( _In_ HANDLE hConsoleInput );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FlushConsoleInputBuffer(HFILE hConsoleInput);
/// Detaches the calling process from its console.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI FreeConsole(void);
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FreeConsole();
/// Sends a specified signal to a console process group that shares the console associated with the calling process.
///
/// The type of signal to be generated. This parameter can be one of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// CTRL_C_EVENT 0
///
/// Generates a CTRL+C signal. This signal cannot be generated for process groups. If dwProcessGroupId is nonzero, this function
/// will succeed, but the CTRL+C signal will not be received by processes within the specified process group.
///
///
/// -
/// CTRL_BREAK_EVENT 1
/// Generates a CTRL+BREAK signal.
///
///
///
///
///
///
/// The identifier of the process group to receive the signal. A process group is created when the CREATE_NEW_PROCESS_GROUP
/// flag is specified in a call to the CreateProcess function. The process identifier of the new process is also the process
/// group identifier of a new process group. The process group includes all processes that are descendants of the root process. Only
/// those processes in the group that share the same console as the calling process receive the signal. In other words, if a process
/// in the group creates a new console, that process does not receive the signal, nor do its descendants.
///
/// If this parameter is zero, the signal is generated in all processes that share the console of the calling process.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI GenerateConsoleCtrlEvent( _In_ DWORD dwCtrlEvent, _In_ DWORD dwProcessGroupId );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GenerateConsoleCtrlEvent(CTRL_EVENT dwCtrlEvent, uint dwProcessGroupId);
/// Gets the background console color from a value.
/// The CHARACTER_ATTRIBUTE value.
/// The extracted background console color.
public static ConsoleColor GetBackgroundColor(this CHARACTER_ATTRIBUTE ca) => (ConsoleColor)(((ushort)ca & 0x00F0) >> 4);
/// Retrieves the text for the specified console alias and executable.
/// The console alias whose text is to be retrieved.
/// A pointer to a buffer that receives the text associated with the console alias.
/// The size of the buffer pointed to by lpTargetBuffer, in bytes.
/// The name of the executable file.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// DWORD WINAPI GetConsoleAlias( _In_ LPTSTR lpSource, _Out_ LPTSTR lpTargetBuffer, _In_ DWORD TargetBufferLength, _In_ LPTSTR
// lpExeName );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleAlias(string lpSource, StringBuilder lpTargetBuffer, uint TargetBufferLength, string lpExeName);
/// Retrieves the text for the specified console alias and executable.
/// The console alias whose text is to be retrieved.
/// A pointer to a buffer that receives the text associated with the console alias.
/// The size of the buffer pointed to by lpTargetBuffer, in bytes.
/// The name of the executable file.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// DWORD WINAPI GetConsoleAlias( _In_ LPTSTR lpSource, _Out_ LPTSTR lpTargetBuffer, _In_ DWORD TargetBufferLength, _In_ LPTSTR
// lpExeName );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleAlias(string lpSource, IntPtr lpTargetBuffer, uint TargetBufferLength, string lpExeName);
/// Retrieves all defined console aliases for the specified executable.
///
/// [out] A pointer to a buffer that receives the aliases.
///
/// The format of the data is as follows: Source1=Target1\0Source2=Target2\0... SourceN=TargetN\0, where N is the number of console
/// aliases defined.
///
///
/// [in] The size of the buffer pointed to by lpAliasBuffer, in bytes.
/// [in] The executable file whose aliases are to be retrieved.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
///
/// To determine the required size for the lpExeName buffer, use the GetConsoleAliasesLength function.
///
/// To compile an application that uses this function, define _WIN32_WINNT as 0x0501 or later. For more information, see
/// Using the Windows Headers.
///
///
// https://docs.microsoft.com/en-us/windows/console/getconsolealiases DWORD WINAPI GetConsoleAliases( _Out_ LPTSTR lpAliasBuffer,
// _In_ DWORD AliasBufferLength, _In_ LPTSTR lpExeName );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("ConsoleApi3.h", MSDNShortId = "92eefa4e-ffde-4886-afde-5aecf450b425")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleAliases(StringBuilder lpAliasBuffer, uint AliasBufferLength, string lpExeName);
/// Retrieves all defined console aliases for the specified executable.
///
/// [out] A pointer to a buffer that receives the aliases.
///
/// The format of the data is as follows: Source1=Target1\0Source2=Target2\0... SourceN=TargetN\0, where N is the number of console
/// aliases defined.
///
///
/// [in] The size of the buffer pointed to by lpAliasBuffer, in bytes.
/// [in] The executable file whose aliases are to be retrieved.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
///
/// To determine the required size for the lpExeName buffer, use the GetConsoleAliasesLength function.
///
/// To compile an application that uses this function, define _WIN32_WINNT as 0x0501 or later. For more information, see
/// Using the Windows Headers.
///
///
// https://docs.microsoft.com/en-us/windows/console/getconsolealiases DWORD WINAPI GetConsoleAliases( _Out_ LPTSTR lpAliasBuffer,
// _In_ DWORD AliasBufferLength, _In_ LPTSTR lpExeName );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("ConsoleApi3.h", MSDNShortId = "92eefa4e-ffde-4886-afde-5aecf450b425")]
public static extern bool GetConsoleAliases(SafeAllocatedMemoryHandle lpAliasBuffer, uint AliasBufferLength, string lpExeName);
/// Retrieves all defined console aliases for the specified executable.
/// The executable file whose aliases are to be retrieved.
/// The aliases.
[PInvokeData("ConsoleApi3.h", MSDNShortId = "92eefa4e-ffde-4886-afde-5aecf450b425")]
public static string[] GetConsoleAliases(string lpExeName)
{
using (var buf = new SafeHGlobalHandle((int)GetConsoleAliasesLength(lpExeName) + StringHelper.GetCharSize()))
{
var ret = GetConsoleAliases(buf, (uint)buf.Size, lpExeName);
if (!ret) Win32Error.ThrowLastError();
return buf.ToStringEnum().ToArray();
}
}
/// Retrieves the required size for the buffer used by the GetConsoleAliases function.
/// The name of the executable file whose console aliases are to be retrieved.
/// The size of the buffer required to store all console aliases defined for this executable file, in bytes.
// DWORD WINAPI GetConsoleAliasesLength( _In_ LPTSTR lpExeName );
[DllImport(Lib.Kernel32, SetLastError = false, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern uint GetConsoleAliasesLength(string lpExeName);
/// Retrieves the names of all executable files with console aliases defined.
/// A pointer to a buffer that receives the names of the executable files.
/// The size of the buffer pointed to by lpExeNameBuffer, in bytes.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// DWORD WINAPI GetConsoleAliasExes( _Out_ LPTSTR lpExeNameBuffer, _In_ DWORD ExeNameBufferLength );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleAliasExes(StringBuilder lpExeNameBuffer, uint ExeNameBufferLength);
/// Retrieves the names of all executable files with console aliases defined.
/// A pointer to a buffer that receives the names of the executable files.
/// The size of the buffer pointed to by lpExeNameBuffer, in bytes.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// DWORD WINAPI GetConsoleAliasExes( _Out_ LPTSTR lpExeNameBuffer, _In_ DWORD ExeNameBufferLength );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleAliasExes(SafeAllocatedMemoryHandle lpExeNameBuffer, uint ExeNameBufferLength);
/// Retrieves the names of all executable files with console aliases defined.
/// An array of the executable files.
// DWORD WINAPI GetConsoleAliasExes( _Out_ LPTSTR lpExeNameBuffer, _In_ DWORD ExeNameBufferLength );
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static string[] GetConsoleAliasExes()
{
using (var buf = new SafeHGlobalHandle((int)(GetConsoleAliasExesLength() + StringHelper.GetCharSize())))
{
var ret = GetConsoleAliasExes(buf, (uint)buf.Size);
// There seems to be a bug in GetConsoleAliasExesLength so this while loop ensures the buffer is large enough
while (!ret && Win32Error.GetLastError() == Win32Error.ERROR_BUFFER_OVERFLOW)
{
buf.Size += 1024;
ret = GetConsoleAliasExes(buf, (uint)buf.Size);
}
if (!ret) Win32Error.ThrowLastError();
return buf.ToStringEnum().ToArray();
}
}
/// Retrieves the required size for the buffer used by the GetConsoleAliasExes function.
///
/// The size of the buffer required to store the names of all executable files that have console aliases defined, in bytes.
///
// DWORD WINAPI GetConsoleAliasExesLength(void);
[DllImport(Lib.Kernel32, SetLastError = false, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern uint GetConsoleAliasExesLength();
///
/// Retrieves the input code page used by the console associated with the calling process. A console uses its input code page to
/// translate keyboard input into the corresponding character value.
///
/// The return value is a code that identifies the code page. For a list of identifiers, see Code Page Identifiers.
// UINT WINAPI GetConsoleCP(void);
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern uint GetConsoleCP();
/// Retrieves information about the size and visibility of the cursor for the specified console screen buffer.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a CONSOLE_CURSOR_INFO structure that receives information about the console's cursor.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI GetConsoleCursorInfo( _In_ HANDLE hConsoleOutput, _Out_ PCONSOLE_CURSOR_INFO lpConsoleCursorInfo );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleCursorInfo(HFILE hConsoleOutput, out CONSOLE_CURSOR_INFO lpConsoleCursorInfo);
/// Retrieves the display mode of the current console.
///
/// The display mode of the console. This parameter can be one or more of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// CONSOLE_FULLSCREEN 1
///
/// Full-screen console. The console is in this mode as soon as the window is maximized. At this point, the transition to
/// full-screen mode can still fail.
///
///
/// -
/// CONSOLE_FULLSCREEN_HARDWARE 2
///
/// Full-screen console communicating directly with the video hardware. This mode is set after the console is in CONSOLE_FULLSCREEN
/// mode to indicate that the transition to full-screen mode has completed.
///
///
///
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI GetConsoleDisplayMode( _Out_ LPDWORD lpModeFlags );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleDisplayMode(out GET_CONSOLE_DISPLAY_MODE lpModeFlags);
/// Retrieves the size of the font used by the specified console screen buffer.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// The index of the font whose size is to be retrieved. This index is obtained by calling the GetCurrentConsoleFont function.
///
///
///
/// If the function succeeds, the return value is a COORD structure that contains the width and height of each character in
/// the font, in logical units. The X member contains the width, while the Y member contains the height.
///
/// If the function fails, the width and the height are zero. To get extended error information, call GetLastError.
///
/// To compile an application that uses this function, define _WIN32_WINNT as 0x0500 or later. For more information, see
/// Using the Windows Headers.
///
///
// COORD WINAPI GetConsoleFontSize( _In_ HANDLE hConsoleOutput, _In_ DWORD nFont );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern COORD GetConsoleFontSize(HFILE hConsoleOutput, uint nFont);
/// Retrieves the history settings for the calling process's console.
///
/// A pointer to a CONSOLE_HISTORY_INFO structure that receives the history settings for the calling process's console.
///
///
/// If the function succeeds the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI GetConsoleHistoryInfo( _Out_ PCONSOLE_HISTORY_INFO lpConsoleHistoryInfo );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleHistoryInfo(ref CONSOLE_HISTORY_INFO lpConsoleHistoryInfo);
/// Retrieves the current input mode of a console's input buffer or the current output mode of a console screen buffer.
///
/// [in] A handle to the console input buffer or the console screen buffer. The handle must have the GENERIC_READ access
/// right. For more information, see Console Buffer Security and Access Rights.
///
///
/// [out] A pointer to a variable that receives the current mode of the specified buffer.
///
/// If the hConsoleHandle parameter is an input handle, the mode can be one or more of the following values. When a console is
/// created, all input modes except ENABLE_WINDOW_INPUT are enabled by default.
///
///
///
/// Value
/// Meaning
///
/// -
/// ENABLE_ECHO_INPUT 0x0004
///
/// Characters read by the ReadFile or ReadConsole function are written to the active screen buffer as they are read. This mode can
/// be used only if the ENABLE_LINE_INPUT mode is also enabled.
///
///
/// -
/// ENABLE_INSERT_MODE 0x0020
///
/// When enabled, text entered in a console window will be inserted at the current cursor location and all text following that
/// location will not be overwritten. When disabled, all following text will be overwritten.
///
///
/// -
/// ENABLE_LINE_INPUT 0x0002
///
/// The ReadFile or ReadConsole function returns only when a carriage return character is read. If this mode is disabled, the
/// functions return when one or more characters are available.
///
///
/// -
/// ENABLE_MOUSE_INPUT 0x0010
///
/// If the mouse pointer is within the borders of the console window and the window has the keyboard focus, mouse events generated
/// by mouse movement and button presses are placed in the input buffer. These events are discarded by ReadFile or ReadConsole, even
/// when this mode is enabled.
///
///
/// -
/// ENABLE_PROCESSED_INPUT 0x0001
///
/// CTRL+C is processed by the system and is not placed in the input buffer. If the input buffer is being read by ReadFile or
/// ReadConsole, other control keys are processed by the system and are not returned in the ReadFile or ReadConsole buffer. If the
/// ENABLE_LINE_INPUT mode is also enabled, backspace, carriage return, and line feed characters are handled by the system.
///
///
/// -
/// ENABLE_QUICK_EDIT_MODE 0x0040
///
/// This flag enables the user to use the mouse to select and edit text. To enable this mode, use . To disable this mode, use
/// ENABLE_EXTENDED_FLAGS without this flag.
///
///
/// -
/// ENABLE_WINDOW_INPUT 0x0008
///
/// User interactions that change the size of the console screen buffer are reported in the console's input buffer. Information
/// about these events can be read from the input buffer by applications using the ReadConsoleInput function, but not by those using
/// ReadFile or ReadConsole.
///
///
/// -
/// ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
///
/// Setting this flag directs the Virtual Terminal processing engine to convert user input received by the console window into
/// Console Virtual Terminal Sequences that can be retrieved by a supporting application through WriteFile or WriteConsole
/// functions. The typical usage of this flag is intended in conjunction with ENABLE_VIRTUAL_TERMINAL_PROCESSING on the output
/// handle to connect to an application that communicates exclusively via virtual terminal sequences.
///
///
///
///
/// If the hConsoleHandle parameter is a screen buffer handle, the mode can be one or more of the following values. When a screen
/// buffer is created, both output modes are enabled by default.
///
///
///
/// Value
/// Meaning
///
/// -
/// ENABLE_PROCESSED_OUTPUT 0x0001
///
/// Characters written by the WriteFile or WriteConsole function or echoed by the ReadFile or ReadConsole function are parsed for
/// ASCII control sequences, and the correct action is performed. Backspace, tab, bell, carriage return, and line feed characters
/// are processed.
///
///
/// -
/// ENABLE_WRAP_AT_EOL_OUTPUT 0x0002
///
/// When writing with WriteFile or WriteConsole or echoing with ReadFile or ReadConsole, the cursor moves to the beginning of the
/// next row when it reaches the end of the current row. This causes the rows displayed in the console window to scroll up
/// automatically when the cursor advances beyond the last row in the window. It also causes the contents of the console screen
/// buffer to scroll up (discarding the top row of the console screen buffer) when the cursor advances beyond the last row in the
/// console screen buffer. If this mode is disabled, the last character in the row is overwritten with any subsequent characters.
///
///
/// -
/// ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
///
/// When writing with WriteFile or WriteConsole, characters are parsed for VT100 and similar control character sequences that
/// control cursor movement, color/font mode, and other operations that can also be performed via the existing Console APIs. For
/// more information, see Console Virtual Terminal Sequences.
///
///
/// -
/// DISABLE_NEWLINE_AUTO_RETURN 0x0008
///
/// When writing with WriteFile or WriteConsole, this adds an additional state to end-of-line wrapping that can delay the cursor
/// move and buffer scroll operations. Normally when ENABLE_WRAP_AT_EOL_OUTPUT is set and text reaches the end of the line, the
/// cursor will immediately move to the next line and the contents of the buffer will scroll up by one line. In contrast with this
/// flag set, the scroll operation and cursor move is delayed until the next character arrives. The written character will be
/// printed in the final position on the line and the cursor will remain above this character as if ENABLE_WRAP_AT_EOL_OUTPUT was
/// off, but the next printable character will be printed as if ENABLE_WRAP_AT_EOL_OUTPUT is on. No overwrite will occur.
/// Specifically, the cursor quickly advances down to the following line, a scroll is performed if necessary, the character is
/// printed, and the cursor advances one more position. The typical usage of this flag is intended in conjunction with setting
/// ENABLE_VIRTUAL_TERMINAL_PROCESSING to better emulate a terminal emulator where writing the final character on the screen (in the
/// bottom right corner) without triggering an immediate scroll is the desired behavior.
///
///
/// -
/// ENABLE_LVB_GRID_WORLDWIDE 0x0010
///
/// The APIs for writing character attributes including WriteConsoleOutput and WriteConsoleOutputAttribute allow the usage of flags
/// from character attributes to adjust the color of the foreground and background of text. Additionally, a range of DBCS flags was
/// specified with the COMMON_LVB prefix. Historically, these flags only functioned in DBCS code pages for Chinese, Japanese, and
/// Korean languages. With exception of the leading byte and trailing byte flags, the remaining flags describing line drawing and
/// reverse video (swap foreground and background colors) can be useful for other languages to emphasize portions of output. With
/// exception of the leading byte and trailing byte flags, the remaining flags describing line drawing and reverse video (swap
/// foreground and background colors) can be useful for other languages to emphasize portions of output. Setting this console mode
/// flag will allow these attributes to be used in every code page on every language. It is off by default to maintain compatibility
/// with known applications that have historically taken advantage of the console ignoring these flags on non-CJK machines to store
/// bits in these fields for their own purposes or by accident. Note that using the ENABLE_VIRTUAL_TERMINAL_PROCESSING mode can
/// result in LVB grid and reverse video flags being set while this flag is still off if the attached application requests
/// underlining or inverse video via Console Virtual Terminal Sequences.
///
///
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
///
///
/// A console consists of an input buffer and one or more screen buffers. The mode of a console buffer determines how the console
/// behaves during input or output (I/O) operations. One set of flag constants is used with input handles, and another set is used
/// with screen buffer (output) handles. Setting the output modes of one screen buffer does not affect the output modes of other
/// screen buffers.
///
///
/// The ENABLE_LINE_INPUT and ENABLE_ECHO_INPUT modes only affect processes that use ReadFile or
/// ReadConsole to read from the console's input buffer. Similarly, the ENABLE_PROCESSED_INPUT mode primarily affects
/// ReadFile and ReadConsole users, except that it also determines whether CTRL+C input is reported in the input
/// buffer (to be read by the ReadConsoleInput function) or is passed to a function defined by the application.
///
///
/// The ENABLE_WINDOW_INPUT and ENABLE_MOUSE_INPUT modes determine whether user interactions involving window resizing
/// and mouse actions are reported in the input buffer or discarded. These events can be read by ReadConsoleInput, but they
/// are always filtered by ReadFile and ReadConsole.
///
///
/// The ENABLE_PROCESSED_OUTPUT and ENABLE_WRAP_AT_EOL_OUTPUT modes only affect processes using ReadFile or
/// ReadConsole and WriteFile or WriteConsole.
///
/// To change a console's I/O modes, call SetConsoleMode function.
///
// https://docs.microsoft.com/en-us/windows/console/getconsolemode BOOL WINAPI GetConsoleMode( _In_ HANDLE hConsoleHandle, _Out_
// LPDWORD lpMode );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("ConsoleApi.h", MSDNShortId = "49adf618-196d-4490-93ca-cd177807f58e")]
public static extern bool GetConsoleMode(HFILE hConsoleHandle, out CONSOLE_INPUT_MODE lpMode);
/// Retrieves the current input mode of a console's input buffer or the current output mode of a console screen buffer.
///
/// [in] A handle to the console input buffer or the console screen buffer. The handle must have the GENERIC_READ access
/// right. For more information, see Console Buffer Security and Access Rights.
///
///
/// [out] A pointer to a variable that receives the current mode of the specified buffer.
///
/// If the hConsoleHandle parameter is an input handle, the mode can be one or more of the following values. When a console is
/// created, all input modes except ENABLE_WINDOW_INPUT are enabled by default.
///
///
///
/// Value
/// Meaning
///
/// -
/// ENABLE_ECHO_INPUT 0x0004
///
/// Characters read by the ReadFile or ReadConsole function are written to the active screen buffer as they are read. This mode can
/// be used only if the ENABLE_LINE_INPUT mode is also enabled.
///
///
/// -
/// ENABLE_INSERT_MODE 0x0020
///
/// When enabled, text entered in a console window will be inserted at the current cursor location and all text following that
/// location will not be overwritten. When disabled, all following text will be overwritten.
///
///
/// -
/// ENABLE_LINE_INPUT 0x0002
///
/// The ReadFile or ReadConsole function returns only when a carriage return character is read. If this mode is disabled, the
/// functions return when one or more characters are available.
///
///
/// -
/// ENABLE_MOUSE_INPUT 0x0010
///
/// If the mouse pointer is within the borders of the console window and the window has the keyboard focus, mouse events generated
/// by mouse movement and button presses are placed in the input buffer. These events are discarded by ReadFile or ReadConsole, even
/// when this mode is enabled.
///
///
/// -
/// ENABLE_PROCESSED_INPUT 0x0001
///
/// CTRL+C is processed by the system and is not placed in the input buffer. If the input buffer is being read by ReadFile or
/// ReadConsole, other control keys are processed by the system and are not returned in the ReadFile or ReadConsole buffer. If the
/// ENABLE_LINE_INPUT mode is also enabled, backspace, carriage return, and line feed characters are handled by the system.
///
///
/// -
/// ENABLE_QUICK_EDIT_MODE 0x0040
///
/// This flag enables the user to use the mouse to select and edit text. To enable this mode, use . To disable this mode, use
/// ENABLE_EXTENDED_FLAGS without this flag.
///
///
/// -
/// ENABLE_WINDOW_INPUT 0x0008
///
/// User interactions that change the size of the console screen buffer are reported in the console's input buffer. Information
/// about these events can be read from the input buffer by applications using the ReadConsoleInput function, but not by those using
/// ReadFile or ReadConsole.
///
///
/// -
/// ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
///
/// Setting this flag directs the Virtual Terminal processing engine to convert user input received by the console window into
/// Console Virtual Terminal Sequences that can be retrieved by a supporting application through WriteFile or WriteConsole
/// functions. The typical usage of this flag is intended in conjunction with ENABLE_VIRTUAL_TERMINAL_PROCESSING on the output
/// handle to connect to an application that communicates exclusively via virtual terminal sequences.
///
///
///
///
/// If the hConsoleHandle parameter is a screen buffer handle, the mode can be one or more of the following values. When a screen
/// buffer is created, both output modes are enabled by default.
///
///
///
/// Value
/// Meaning
///
/// -
/// ENABLE_PROCESSED_OUTPUT 0x0001
///
/// Characters written by the WriteFile or WriteConsole function or echoed by the ReadFile or ReadConsole function are parsed for
/// ASCII control sequences, and the correct action is performed. Backspace, tab, bell, carriage return, and line feed characters
/// are processed.
///
///
/// -
/// ENABLE_WRAP_AT_EOL_OUTPUT 0x0002
///
/// When writing with WriteFile or WriteConsole or echoing with ReadFile or ReadConsole, the cursor moves to the beginning of the
/// next row when it reaches the end of the current row. This causes the rows displayed in the console window to scroll up
/// automatically when the cursor advances beyond the last row in the window. It also causes the contents of the console screen
/// buffer to scroll up (discarding the top row of the console screen buffer) when the cursor advances beyond the last row in the
/// console screen buffer. If this mode is disabled, the last character in the row is overwritten with any subsequent characters.
///
///
/// -
/// ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
///
/// When writing with WriteFile or WriteConsole, characters are parsed for VT100 and similar control character sequences that
/// control cursor movement, color/font mode, and other operations that can also be performed via the existing Console APIs. For
/// more information, see Console Virtual Terminal Sequences.
///
///
/// -
/// DISABLE_NEWLINE_AUTO_RETURN 0x0008
///
/// When writing with WriteFile or WriteConsole, this adds an additional state to end-of-line wrapping that can delay the cursor
/// move and buffer scroll operations. Normally when ENABLE_WRAP_AT_EOL_OUTPUT is set and text reaches the end of the line, the
/// cursor will immediately move to the next line and the contents of the buffer will scroll up by one line. In contrast with this
/// flag set, the scroll operation and cursor move is delayed until the next character arrives. The written character will be
/// printed in the final position on the line and the cursor will remain above this character as if ENABLE_WRAP_AT_EOL_OUTPUT was
/// off, but the next printable character will be printed as if ENABLE_WRAP_AT_EOL_OUTPUT is on. No overwrite will occur.
/// Specifically, the cursor quickly advances down to the following line, a scroll is performed if necessary, the character is
/// printed, and the cursor advances one more position. The typical usage of this flag is intended in conjunction with setting
/// ENABLE_VIRTUAL_TERMINAL_PROCESSING to better emulate a terminal emulator where writing the final character on the screen (in the
/// bottom right corner) without triggering an immediate scroll is the desired behavior.
///
///
/// -
/// ENABLE_LVB_GRID_WORLDWIDE 0x0010
///
/// The APIs for writing character attributes including WriteConsoleOutput and WriteConsoleOutputAttribute allow the usage of flags
/// from character attributes to adjust the color of the foreground and background of text. Additionally, a range of DBCS flags was
/// specified with the COMMON_LVB prefix. Historically, these flags only functioned in DBCS code pages for Chinese, Japanese, and
/// Korean languages. With exception of the leading byte and trailing byte flags, the remaining flags describing line drawing and
/// reverse video (swap foreground and background colors) can be useful for other languages to emphasize portions of output. With
/// exception of the leading byte and trailing byte flags, the remaining flags describing line drawing and reverse video (swap
/// foreground and background colors) can be useful for other languages to emphasize portions of output. Setting this console mode
/// flag will allow these attributes to be used in every code page on every language. It is off by default to maintain compatibility
/// with known applications that have historically taken advantage of the console ignoring these flags on non-CJK machines to store
/// bits in these fields for their own purposes or by accident. Note that using the ENABLE_VIRTUAL_TERMINAL_PROCESSING mode can
/// result in LVB grid and reverse video flags being set while this flag is still off if the attached application requests
/// underlining or inverse video via Console Virtual Terminal Sequences.
///
///
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
///
///
/// A console consists of an input buffer and one or more screen buffers. The mode of a console buffer determines how the console
/// behaves during input or output (I/O) operations. One set of flag constants is used with input handles, and another set is used
/// with screen buffer (output) handles. Setting the output modes of one screen buffer does not affect the output modes of other
/// screen buffers.
///
///
/// The ENABLE_LINE_INPUT and ENABLE_ECHO_INPUT modes only affect processes that use ReadFile or
/// ReadConsole to read from the console's input buffer. Similarly, the ENABLE_PROCESSED_INPUT mode primarily affects
/// ReadFile and ReadConsole users, except that it also determines whether CTRL+C input is reported in the input
/// buffer (to be read by the ReadConsoleInput function) or is passed to a function defined by the application.
///
///
/// The ENABLE_WINDOW_INPUT and ENABLE_MOUSE_INPUT modes determine whether user interactions involving window resizing
/// and mouse actions are reported in the input buffer or discarded. These events can be read by ReadConsoleInput, but they
/// are always filtered by ReadFile and ReadConsole.
///
///
/// The ENABLE_PROCESSED_OUTPUT and ENABLE_WRAP_AT_EOL_OUTPUT modes only affect processes using ReadFile or
/// ReadConsole and WriteFile or WriteConsole.
///
/// To change a console's I/O modes, call SetConsoleMode function.
///
// https://docs.microsoft.com/en-us/windows/console/getconsolemode BOOL WINAPI GetConsoleMode( _In_ HANDLE hConsoleHandle, _Out_
// LPDWORD lpMode );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("ConsoleApi.h", MSDNShortId = "49adf618-196d-4490-93ca-cd177807f58e")]
public static extern bool GetConsoleMode(HFILE hConsoleHandle, out CONSOLE_OUTPUT_MODE lpMode);
/// Retrieves the original title for the current console window.
///
/// A pointer to a buffer that receives a null-terminated string containing the original title.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
/// The size of the lpConsoleTitle buffer, in characters.
///
/// If the function succeeds, the return value is the length of the string copied to the buffer, in characters.
/// If the buffer is not large enough to store the title, the return value is zero and GetLastError returns ERROR_SUCCESS.
/// If the function fails, the return value is zero and GetLastError returns the error code.
///
// DWORD WINAPI GetConsoleOriginalTitle( _Out_ LPTSTR lpConsoleTitle, _In_ DWORD nSize );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern uint GetConsoleOriginalTitle(StringBuilder lpConsoleTitle, uint nSize);
/// Retrieves the original title for the current console window.
///
/// A pointer to a buffer that receives a null-terminated string containing the original title.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
/// The size of the lpConsoleTitle buffer, in characters.
///
/// If the function succeeds, the return value is the length of the string copied to the buffer, in characters.
/// If the buffer is not large enough to store the title, the return value is zero and GetLastError returns ERROR_SUCCESS.
/// If the function fails, the return value is zero and GetLastError returns the error code.
///
// DWORD WINAPI GetConsoleOriginalTitle( _Out_ LPTSTR lpConsoleTitle, _In_ DWORD nSize );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern uint GetConsoleOriginalTitle(IntPtr lpConsoleTitle, uint nSize);
///
/// Retrieves the output code page used by the console associated with the calling process. A console uses its output code page to
/// translate the character values written by the various output functions into the images displayed in the console window.
///
/// The return value is a code that identifies the code page. For a list of identifiers, see Code Page Identifiers.
// UINT WINAPI GetConsoleOutputCP(void);
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern uint GetConsoleOutputCP();
/// Retrieves a list of the processes attached to the current console.
///
/// A pointer to a buffer that receives an array of process identifiers upon success.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
/// The maximum number of process identifiers that can be stored in the lpdwProcessList buffer.
///
///
/// If the function succeeds, the return value is less than or equal to dwProcessCount and represents the number of process
/// identifiers stored in the lpdwProcessList buffer.
///
///
/// If the buffer is too small to hold all the valid process identifiers, the return value is the required number of array elements.
/// The function will have stored no identifiers in the buffer. In this situation, use the return value to allocate a buffer that is
/// large enough to store the entire list and call the function again.
///
///
/// If the return value is zero, the function has failed, because every console has at least one process associated with it. To get
/// extended error information, call GetLastError.
///
///
// DWORD WINAPI GetConsoleProcessList( _Out_ LPDWORD lpdwProcessList, _In_ DWORD dwProcessCount );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern uint GetConsoleProcessList([Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] uint[] lpdwProcessList, uint dwProcessCount);
/// Retrieves a list of the processes attached to the current console.
/// An array of process identifiers upon success.
public static uint[] GetConsoleProcessList()
{
uint c = 8U, cnt;
uint[] output;
do
{
cnt = c;
output = new uint[cnt];
c = GetConsoleProcessList(output, cnt);
} while (c > cnt);
Array.Resize(ref output, (int)c);
return output;
}
/// Retrieves information about the specified console screen buffer.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a CONSOLE_SCREEN_BUFFER_INFO structure that receives the console screen buffer information.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI GetConsoleScreenBufferInfo( _In_ HANDLE hConsoleOutput, _Out_ PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleScreenBufferInfo(HFILE hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
/// Retrieves extended information about the specified console screen buffer.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A CONSOLE_SCREEN_BUFFER_INFOEX structure that receives the requested console screen buffer information.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI GetConsoleScreenBufferInfoEx( _In_ HANDLE hConsoleOutput, _Out_ PCONSOLE_SCREEN_BUFFER_INFOEX
// lpConsoleScreenBufferInfoEx );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleScreenBufferInfoEx(HFILE hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFOEX lpConsoleScreenBufferInfoEx);
/// Retrieves information about the current console selection.
/// A pointer to a CONSOLE_SELECTION_INFO structure that receives the selection information.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI GetConsoleSelectionInfo( _Out_ PCONSOLE_SELECTION_INFO lpConsoleSelectionInfo );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetConsoleSelectionInfo(out CONSOLE_SELECTION_INFO lpConsoleSelectionInfo);
/// Retrieves the title for the current console window.
///
///
/// A pointer to a buffer that receives a null-terminated string containing the title. If the buffer is too small to store the
/// title, the function stores as many characters of the title as will fit in the buffer, ending with a null terminator.
///
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
/// The size of the buffer pointed to by the lpConsoleTitle parameter, in characters.
///
/// If the function succeeds, the return value is the length of the console window's title, in characters.
/// If the function fails, the return value is zero and GetLastError returns the error code.
///
// DWORD WINAPI GetConsoleTitle( _Out_ LPTSTR lpConsoleTitle, _In_ DWORD nSize );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern uint GetConsoleTitle(StringBuilder lpConsoleTitle, uint nSize);
/// Retrieves the title for the current console window.
///
///
/// A pointer to a buffer that receives a null-terminated string containing the title. If the buffer is too small to store the
/// title, the function stores as many characters of the title as will fit in the buffer, ending with a null terminator.
///
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
/// The size of the buffer pointed to by the lpConsoleTitle parameter, in characters.
///
/// If the function succeeds, the return value is the length of the console window's title, in characters.
/// If the function fails, the return value is zero and GetLastError returns the error code.
///
// DWORD WINAPI GetConsoleTitle( _Out_ LPTSTR lpConsoleTitle, _In_ DWORD nSize );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern uint GetConsoleTitle(IntPtr lpConsoleTitle, uint nSize);
/// Retrieves the window handle used by the console associated with the calling process.
///
/// The return value is a handle to the window used by the console associated with the calling process or NULL if there is no
/// such associated console.
///
// HWND WINAPI GetConsoleWindow(void);
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern HWND GetConsoleWindow();
/// Retrieves information about the current console font.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// If this parameter is TRUE, font information is retrieved for the maximum window size. If this parameter is FALSE,
/// font information is retrieved for the current window size.
///
/// A pointer to a CONSOLE_FONT_INFO structure that receives the requested font information.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI GetCurrentConsoleFont( _In_ HANDLE hConsoleOutput, _In_ BOOL bMaximumWindow, _Out_ PCONSOLE_FONT_INFO
// lpConsoleCurrentFont );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCurrentConsoleFont(HFILE hConsoleOutput, [MarshalAs(UnmanagedType.Bool)] bool bMaximumWindow, out CONSOLE_FONT_INFO lpConsoleCurrentFont);
/// Retrieves extended information about the current console font.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// If this parameter is TRUE, font information is retrieved for the maximum window size. If this parameter is FALSE,
/// font information is retrieved for the current window size.
///
/// A pointer to a CONSOLE_FONT_INFOEX structure that receives the requested font information.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI GetCurrentConsoleFontEx( _In_ HANDLE hConsoleOutput, _In_ BOOL bMaximumWindow, _Out_ PCONSOLE_FONT_INFOEX
// lpConsoleCurrentFontEx );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern bool GetCurrentConsoleFontEx(HFILE hConsoleOutput, [MarshalAs(UnmanagedType.Bool)] bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);
/// Gets the foreground console color from a value.
/// The CHARACTER_ATTRIBUTE value.
/// The extracted foreground console color.
public static ConsoleColor GetForegroundColor(this CHARACTER_ATTRIBUTE ca) => (ConsoleColor)((ushort)ca & 0x000F);
/// Retrieves the size of the largest possible console window, based on the current font and the size of the display.
/// A handle to the console screen buffer.
///
///
/// If the function succeeds, the return value is a COORD structure that specifies the number of character cell rows (
/// X member) and columns ( Y member) in the largest possible console window. Otherwise, the members of the structure
/// are zero.
///
/// To get extended error information, call GetLastError.
///
// COORD WINAPI GetLargestConsoleWindowSize( _In_ HANDLE hConsoleOutput );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern COORD GetLargestConsoleWindowSize(HFILE hConsoleOutput);
/// Retrieves the number of unread input records in the console's input buffer.
///
/// A handle to the console input buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a variable that receives the number of unread input records in the console's input buffer.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI GetNumberOfConsoleInputEvents( _In_ HANDLE hConsoleInput, _Out_ LPDWORD lpcNumberOfEvents );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetNumberOfConsoleInputEvents(HFILE hConsoleInput, out uint lpcNumberOfEvents);
/// Retrieves the number of buttons on the mouse used by the current console.
/// A pointer to a variable that receives the number of mouse buttons.
///
/// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.To get extended error
/// information, call GetLastError.
///
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetNumberOfConsoleMouseButtons(out uint lpNumberOfMouseButtons);
/// Reads data from the specified console input buffer without removing it from the buffer.
///
/// A handle to the console input buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to an array of INPUT_RECORD structures that receives the input buffer data.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
/// The size of the array pointed to by the lpBuffer parameter, in array elements.
/// A pointer to a variable that receives the number of input records read.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI PeekConsoleInput( _In_ HANDLE hConsoleInput, _Out_ PINPUT_RECORD lpBuffer, _In_ DWORD nLength, _Out_ LPDWORD
// lpNumberOfEventsRead );
[DllImport(Lib.Kernel32, SetLastError = true, EntryPoint = "PeekConsoleInputW", CharSet = CharSet.Unicode)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PeekConsoleInput(HFILE hConsoleInput, [Out] INPUT_RECORD[] lpBuffer, uint nLength, out uint lpNumberOfEventsRead);
/// Reads character input from the console input buffer and removes it from the buffer.
///
/// A handle to the console input buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a buffer that receives the data read from the console input buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
///
/// The number of characters to be read. The size of the buffer pointed to by the lpBuffer parameter should be at least bytes.
///
/// A pointer to a variable that receives the number of characters actually read.
///
///
/// A pointer to a CONSOLE_READCONSOLE_CONTROL structure that specifies a control character to signal the end of the read
/// operation. This parameter can be NULL.
///
/// This parameter requires Unicode input by default. For ANSI mode, set this parameter to NULL.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ReadConsole( _In_ HANDLE hConsoleInput, _Out_ LPVOID lpBuffer, _In_ DWORD nNumberOfCharsToRead, _Out_ LPDWORD
// lpNumberOfCharsRead, _In_opt_ LPVOID pInputControl );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadConsole(HFILE hConsoleInput, StringBuilder lpBuffer, uint nNumberOfCharsToRead, out uint lpNumberOfCharsRead, IntPtr pInputControl = default);
/// Reads character input from the console input buffer and removes it from the buffer.
///
/// A handle to the console input buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a buffer that receives the data read from the console input buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
///
/// The number of characters to be read. The size of the buffer pointed to by the lpBuffer parameter should be at least bytes.
///
/// A pointer to a variable that receives the number of characters actually read.
///
///
/// A pointer to a CONSOLE_READCONSOLE_CONTROL structure that specifies a control character to signal the end of the read
/// operation. This parameter can be NULL.
///
/// This parameter requires Unicode input by default. For ANSI mode, set this parameter to NULL.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ReadConsole( _In_ HANDLE hConsoleInput, _Out_ LPVOID lpBuffer, _In_ DWORD nNumberOfCharsToRead, _Out_ LPDWORD
// lpNumberOfCharsRead, _In_opt_ LPVOID pInputControl );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadConsole(HFILE hConsoleInput, StringBuilder lpBuffer, uint nNumberOfCharsToRead, out uint lpNumberOfCharsRead, in CONSOLE_READCONSOLE_CONTROL pInputControl);
/// Reads character input from the console input buffer and removes it from the buffer.
///
/// A handle to the console input buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a buffer that receives the data read from the console input buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
///
/// The number of characters to be read. The size of the buffer pointed to by the lpBuffer parameter should be at least bytes.
///
/// A pointer to a variable that receives the number of characters actually read.
///
///
/// A pointer to a CONSOLE_READCONSOLE_CONTROL structure that specifies a control character to signal the end of the read
/// operation. This parameter can be NULL.
///
/// This parameter requires Unicode input by default. For ANSI mode, set this parameter to NULL.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ReadConsole( _In_ HANDLE hConsoleInput, _Out_ LPVOID lpBuffer, _In_ DWORD nNumberOfCharsToRead, _Out_ LPDWORD
// lpNumberOfCharsRead, _In_opt_ LPVOID pInputControl );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadConsole(HFILE hConsoleInput, IntPtr lpBuffer, uint nNumberOfCharsToRead, out uint lpNumberOfCharsRead, IntPtr pInputControl = default);
/// Reads character input from the console input buffer and removes it from the buffer.
///
/// A handle to the console input buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a buffer that receives the data read from the console input buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
///
/// The number of characters to be read. The size of the buffer pointed to by the lpBuffer parameter should be at least bytes.
///
/// A pointer to a variable that receives the number of characters actually read.
///
///
/// A pointer to a CONSOLE_READCONSOLE_CONTROL structure that specifies a control character to signal the end of the read
/// operation. This parameter can be NULL.
///
/// This parameter requires Unicode input by default. For ANSI mode, set this parameter to NULL.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ReadConsole( _In_ HANDLE hConsoleInput, _Out_ LPVOID lpBuffer, _In_ DWORD nNumberOfCharsToRead, _Out_ LPDWORD
// lpNumberOfCharsRead, _In_opt_ LPVOID pInputControl );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadConsole(HFILE hConsoleInput, IntPtr lpBuffer, uint nNumberOfCharsToRead, out uint lpNumberOfCharsRead, in CONSOLE_READCONSOLE_CONTROL pInputControl);
/// Reads data from a console input buffer and removes it from the buffer.
///
/// A handle to the console input buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to an array of INPUT_RECORD structures that receives the input buffer data.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
/// The size of the array pointed to by the lpBuffer parameter, in array elements.
/// A pointer to a variable that receives the number of input records read.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ReadConsoleInput( _In_ HANDLE hConsoleInput, _Out_ PINPUT_RECORD lpBuffer, _In_ DWORD nLength, _Out_ LPDWORD
// lpNumberOfEventsRead );
[DllImport(Lib.Kernel32, SetLastError = true, EntryPoint = "ReadConsoleInputW", CharSet = CharSet.Unicode)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadConsoleInput(HFILE hConsoleInput, [Out] INPUT_RECORD[] lpBuffer, uint nLength, out uint lpNumberOfEventsRead);
///
/// Reads character and color attribute data from a rectangular block of character cells in a console screen buffer, and the
/// function writes the data to a rectangular block at a specified location in the destination buffer.
///
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
///
/// A pointer to a destination buffer that receives the data read from the console screen buffer. This pointer is treated as the
/// origin of a two-dimensional array of CHAR_INFO structures whose size is specified by the dwBufferSize parameter.
///
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
///
/// The size of the lpBuffer parameter, in character cells. The X member of the COORD structure is the number of
/// columns; the Y member is the number of rows.
///
///
/// The coordinates of the upper-left cell in the lpBuffer parameter that receives the data read from the console screen buffer. The
/// X member of the COORD structure is the column, and the Y member is the row.
///
///
/// A pointer to a SMALL_RECT structure. On input, the structure members specify the upper-left and lower-right coordinates
/// of the console screen buffer rectangle from which the function is to read. On output, the structure members specify the actual
/// rectangle that was used.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ReadConsoleOutput( _In_ HANDLE hConsoleOutput, _Out_ PCHAR_INFO lpBuffer, _In_ COORD dwBufferSize, _In_ COORD
// dwBufferCoord, _Inout_ PSMALL_RECT lpReadRegion );
[DllImport(Lib.Kernel32, SetLastError = true, EntryPoint = "ReadConsoleOutputW", CharSet = CharSet.Unicode)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadConsoleOutput(HFILE hConsoleOutput, [Out] CHAR_INFO[] lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpReadRegion);
///
/// Copies a specified number of character attributes from consecutive cells of a console screen buffer, beginning at a specified location.
///
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a buffer that receives the attributes being used by the console screen buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
/// For more information, see Character Attributes.
///
///
/// The number of screen buffer character cells from which to read. The size of the buffer pointed to by the lpAttribute parameter
/// should be .
///
///
/// The coordinates of the first cell in the console screen buffer from which to read, in characters. The X member of the
/// COORD structure is the column, and the Y member is the row.
///
/// A pointer to a variable that receives the number of attributes actually read.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ReadConsoleOutputAttribute( _In_ HANDLE hConsoleOutput, _Out_ LPWORD lpAttribute, _In_ DWORD nLength, _In_ COORD
// dwReadCoord, _Out_ LPDWORD lpNumberOfAttrsRead );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadConsoleOutputAttribute(HFILE hConsoleOutput, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] CHARACTER_ATTRIBUTE[] lpAttribute, uint nLength, COORD dwReadCoord, out uint lpNumberOfAttrsRead);
/// Copies a number of characters from consecutive cells of a console screen buffer, beginning at a specified location.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a buffer that receives the characters read from the console screen buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size.The maximum size of the buffer
/// will depend on heap usage.
///
///
///
/// The number of screen buffer character cells from which to read. The size of the buffer pointed to by the lpCharacter parameter
/// should be nLength * sizeof(TCHAR).
///
///
/// The coordinates of the first cell in the console screen buffer from which to read, in characters. The X member of the COORD
/// structure is the column, and the Y member is the row.
///
/// A pointer to a variable that receives the number of characters actually read.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ReadConsoleOutputCharacter(_In_ HANDLE hConsoleOutput, _Out_ LPTSTR lpCharacter, _In_ DWORD nLength, _In_ COORD
// dwReadCoord, _Out_ LPDWORD lpNumberOfCharsRead);
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadConsoleOutputCharacter(HFILE hConsoleOutput, StringBuilder lpCharacter, uint nLength, COORD dwReadCoord, out uint lpNumberOfCharsRead);
/// Copies a number of characters from consecutive cells of a console screen buffer, beginning at a specified location.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a buffer that receives the characters read from the console screen buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size.The maximum size of the buffer
/// will depend on heap usage.
///
///
///
/// The number of screen buffer character cells from which to read. The size of the buffer pointed to by the lpCharacter parameter
/// should be nLength * sizeof(TCHAR).
///
///
/// The coordinates of the first cell in the console screen buffer from which to read, in characters. The X member of the COORD
/// structure is the column, and the Y member is the row.
///
/// A pointer to a variable that receives the number of characters actually read.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ReadConsoleOutputCharacter(_In_ HANDLE hConsoleOutput, _Out_ LPTSTR lpCharacter, _In_ DWORD nLength, _In_ COORD
// dwReadCoord, _Out_ LPDWORD lpNumberOfCharsRead);
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadConsoleOutputCharacter(HFILE hConsoleOutput, IntPtr lpCharacter, uint nLength, COORD dwReadCoord, out uint lpNumberOfCharsRead);
/// Resizes the internal buffers for a pseudoconsole to the given size.
/// [in] A handle to an active psuedoconsole as opened by CreatePseudoConsole.
///
/// [in] The dimensions of the window/buffer in count of characters that will be used for the internal buffer of this pseudoconsole.
///
///
/// Type: HRESULT
/// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
///
///
/// This function can resize the internal buffers in the pseudoconsole session to match the window/buffer size being used for
/// display on the terminal end. This ensures that attached Command-Line Interface (CUI) applications using the Console Functions to
/// communicate will have the correct dimensions returned in their calls.
///
// https://docs.microsoft.com/en-us/windows/console/resizepseudoconsole HRESULT WINAPI ResizePseudoConsole( _In_ HPCON hPC , _In_
// COORD size );
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ConsoleApi.h")]
public static extern HRESULT ResizePseudoConsole([In] HPCON hPC, [In] COORD size);
///
/// Moves a block of data in a screen buffer. The effects of the move can be limited by specifying a clipping rectangle, so the
/// contents of the console screen buffer outside the clipping rectangle are unchanged.
///
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a SMALL_RECT structure whose members specify the upper-left and lower-right coordinates of the console
/// screen buffer rectangle to be moved.
///
///
/// A pointer to a SMALL_RECT structure whose members specify the upper-left and lower-right coordinates of the console
/// screen buffer rectangle that is affected by the scrolling. This pointer can be NULL.
///
///
/// A COORD structure that specifies the upper-left corner of the new location of the lpScrollRectangle contents, in characters.
///
///
/// A pointer to a CHAR_INFO structure that specifies the character and color attributes to be used in filling the cells
/// within the intersection of lpScrollRectangle and lpClipRectangle that were left empty as a result of the move.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ScrollConsoleScreenBuffer( _In_ HANDLE hConsoleOutput, _In_ const SMALL_RECT *lpScrollRectangle, _In_opt_ const SMALL_RECT
// *lpClipRectangle, _In_ COORD dwDestinationOrigin, _In_ const CHAR_INFO *lpFill );
[DllImport(Lib.Kernel32, SetLastError = true, EntryPoint = "ScrollConsoleScreenBufferW", CharSet = CharSet.Unicode)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ScrollConsoleScreenBuffer(HFILE hConsoleOutput, in SMALL_RECT lpScrollRectangle, in SMALL_RECT lpClipRectangle, COORD dwDestinationOrigin, in CHAR_INFO lpFill);
///
/// Moves a block of data in a screen buffer. The effects of the move can be limited by specifying a clipping rectangle, so the
/// contents of the console screen buffer outside the clipping rectangle are unchanged.
///
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a SMALL_RECT structure whose members specify the upper-left and lower-right coordinates of the console
/// screen buffer rectangle to be moved.
///
///
/// A pointer to a SMALL_RECT structure whose members specify the upper-left and lower-right coordinates of the console
/// screen buffer rectangle that is affected by the scrolling. This pointer can be NULL.
///
///
/// A COORD structure that specifies the upper-left corner of the new location of the lpScrollRectangle contents, in characters.
///
///
/// A pointer to a CHAR_INFO structure that specifies the character and color attributes to be used in filling the cells
/// within the intersection of lpScrollRectangle and lpClipRectangle that were left empty as a result of the move.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI ScrollConsoleScreenBuffer( _In_ HANDLE hConsoleOutput, _In_ const SMALL_RECT *lpScrollRectangle, _In_opt_ const SMALL_RECT
// *lpClipRectangle, _In_ COORD dwDestinationOrigin, _In_ const CHAR_INFO *lpFill );
[DllImport(Lib.Kernel32, SetLastError = true, EntryPoint = "ScrollConsoleScreenBufferW", CharSet = CharSet.Unicode)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ScrollConsoleScreenBuffer(HFILE hConsoleOutput, in SMALL_RECT lpScrollRectangle, [Optional] IntPtr lpClipRectangle, COORD dwDestinationOrigin, in CHAR_INFO lpFill);
/// Sets the specified screen buffer to be the currently displayed console screen buffer.
/// A handle to the console screen buffer.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleActiveScreenBuffer( _In_ HANDLE hConsoleOutput );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleActiveScreenBuffer(HFILE hConsoleOutput);
///
/// Sets the input code page used by the console associated with the calling process. A console uses its input code page to
/// translate keyboard input into the corresponding character value.
///
/// The identifier of the code page to be set. For more information, see Remarks.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleCP( _In_ UINT wCodePageID );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleCP(uint wCodePageID);
///
///
/// Adds or removes an application-defined HandlerRoutine function from the list of handler functions for the calling process.
///
///
/// If no handler function is specified, the function sets an inheritable attribute that determines whether the calling process
/// ignores CTRL+C signals.
///
///
///
/// A pointer to the application-defined HandlerRoutine function to be added or removed. This parameter can be NULL.
///
///
/// If this parameter is TRUE, the handler is added; if it is FALSE, the handler is removed.
///
/// If the HandlerRoutine parameter is NULL, a TRUE value causes the calling process to ignore CTRL+C input, and a
/// FALSE value restores normal processing of CTRL+C input. This attribute of ignoring or processing CTRL+C is inherited by
/// child processes.
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleCtrlHandler( _In_opt_ PHANDLER_ROUTINE HandlerRoutine, _In_ BOOL Add );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine HandlerRoutine, [MarshalAs(UnmanagedType.Bool)] bool Add);
/// Sets the size and visibility of the cursor for the specified console screen buffer.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a CONSOLE_CURSOR_INFO structure that provides the new specifications for the console screen buffer's cursor.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleCursorInfo( _In_ HANDLE hConsoleOutput, _In_ const CONSOLE_CURSOR_INFO *lpConsoleCursorInfo );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleCursorInfo(HFILE hConsoleOutput, in CONSOLE_CURSOR_INFO lpConsoleCursorInfo);
///
/// Sets the cursor position in the specified console screen buffer.
///
///
///
/// [in]A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
///
///
/// [in]A COORD structure that specifies the new cursor position, in characters. The coordinates are the column and row of a
/// screen buffer character cell. The coordinates must be within the boundaries of the console screen buffer.
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
///
///
/// The cursor position determines where characters written by the WriteFile or WriteConsole function, or echoed by
/// the ReadFile or ReadConsole function, are displayed. To determine the current position of the cursor, use the
/// GetConsoleScreenBufferInfo function.
///
///
/// If the new cursor position is not within the boundaries of the console screen buffer's window, the window origin changes to make
/// the cursor visible.
///
///
// https://docs.microsoft.com/en-us/windows/console/setconsolecursorposition BOOL WINAPI SetConsoleCursorPosition( _In_ HANDLE
// hConsoleOutput, _In_ COORD dwCursorPosition );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "8e9abada-a64e-429f-8286-ced1169c7104")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleCursorPosition(HFILE hConsoleOutput, COORD dwCursorPosition);
/// Sets the display mode of the specified console screen buffer.
/// A handle to the console screen buffer.
///
/// The display mode of the console. This parameter can be one or more of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// CONSOLE_FULLSCREEN_MODE 1
/// Text is displayed in full-screen mode.
///
/// -
/// CONSOLE_WINDOWED_MODE 2
/// Text is displayed in a console window.
///
///
///
///
///
/// A pointer to a COORD structure that receives the new dimensions of the screen buffer, in characters.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleDisplayMode( _In_ HANDLE hConsoleOutput, _In_ DWORD dwFlags, _Out_opt_ PCOORD lpNewScreenBufferDimensions );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleDisplayMode(HFILE hConsoleOutput, SET_CONSOLE_DISPLAY_MODE dwFlags, out COORD lpNewScreenBufferDimensions);
/// Sets the history settings for the calling process's console.
///
/// A pointer to a CONSOLE_HISTORY_INFO structure that contains the history settings for the process's console.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleHistoryInfo( _In_ PCONSOLE_HISTORY_INFO lpConsoleHistoryInfo );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleHistoryInfo(in CONSOLE_HISTORY_INFO lpConsoleHistoryInfo);
/// Sets the input mode of a console's input buffer or the output mode of a console screen buffer.
///
/// A handle to the console input buffer or a console screen buffer. The handle must have the GENERIC_READ access right. For
/// more information, see Console Buffer Security and Access Rights.
///
///
///
/// The input or output mode to be set. If the hConsoleHandle parameter is an input handle, the mode can be one or more of the
/// following values. When a console is created, all input modes except ENABLE_WINDOW_INPUT are enabled by default.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// ENABLE_ECHO_INPUT 0x0004
///
/// Characters read by the ReadFile or ReadConsole function are written to the active screen buffer as they are read. This mode can
/// be used only if the ENABLE_LINE_INPUT mode is also enabled.
///
///
/// -
/// ENABLE_EXTENDED_FLAGS 0x0080
/// Required to enable or disable extended flags. See ENABLE_INSERT_MODE and ENABLE_QUICK_EDIT_MODE.
///
/// -
/// ENABLE_INSERT_MODE 0x0020
///
/// When enabled, text entered in a console window will be inserted at the current cursor location and all text following that
/// location will not be overwritten. When disabled, all following text will be overwritten. To enable this mode, use . To disable
/// this mode, use ENABLE_EXTENDED_FLAGS without this flag.
///
///
/// -
/// ENABLE_LINE_INPUT 0x0002
///
/// The ReadFile or ReadConsole function returns only when a carriage return character is read. If this mode is disabled, the
/// functions return when one or more characters are available.
///
///
/// -
/// ENABLE_MOUSE_INPUT 0x0010
///
/// If the mouse pointer is within the borders of the console window and the window has the keyboard focus, mouse events generated
/// by mouse movement and button presses are placed in the input buffer. These events are discarded by ReadFile or ReadConsole, even
/// when this mode is enabled.
///
///
/// -
/// ENABLE_PROCESSED_INPUT 0x0001
///
/// CTRL+C is processed by the system and is not placed in the input buffer. If the input buffer is being read by ReadFile or
/// ReadConsole, other control keys are processed by the system and are not returned in the ReadFile or ReadConsole buffer. If the
/// ENABLE_LINE_INPUT mode is also enabled, backspace, carriage return, and line feed characters are handled by the system.
///
///
/// -
/// ENABLE_QUICK_EDIT_MODE 0x0040
///
/// This flag enables the user to use the mouse to select and edit text. To enable this mode, use . To disable this mode, use
/// ENABLE_EXTENDED_FLAGS without this flag.
///
///
/// -
/// ENABLE_WINDOW_INPUT 0x0008
///
/// User interactions that change the size of the console screen buffer are reported in the console's input buffer. Information
/// about these events can be read from the input buffer by applications using the ReadConsoleInput function, but not by those using
/// ReadFile or ReadConsole.
///
///
/// -
/// ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
///
/// Setting this flag directs the Virtual Terminal processing engine to convert user input received by the console window into
/// Console Virtual Terminal Sequences that can be retrieved by a supporting application through ReadFile or ReadConsole functions.
/// The typical usage of this flag is intended in conjunction with ENABLE_VIRTUAL_TERMINAL_PROCESSING on the output handle to
/// connect to an application that communicates exclusively via virtual terminal sequences.
///
///
///
///
///
/// If the hConsoleHandle parameter is a screen buffer handle, the mode can be one or more of the following values. When a screen
/// buffer is created, both output modes are enabled by default.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// ENABLE_PROCESSED_OUTPUT 0x0001
///
/// Characters written by the WriteFile or WriteConsole function or echoed by the ReadFile or ReadConsole function are examined for
/// ASCII control sequences and the correct action is performed. Backspace, tab, bell, carriage return, and line feed characters are processed.
///
///
/// -
/// ENABLE_WRAP_AT_EOL_OUTPUT 0x0002
///
/// When writing with WriteFile or WriteConsole or echoing with ReadFile or ReadConsole, the cursor moves to the beginning of the
/// next row when it reaches the end of the current row. This causes the rows displayed in the console window to scroll up
/// automatically when the cursor advances beyond the last row in the window. It also causes the contents of the console screen
/// buffer to scroll up (discarding the top row of the console screen
/// buffer) when the cursor advances beyond the last row in the console screen buffer. If this mode is disabled, the last character
/// in the row is overwritten with any subsequent characters.
///
///
/// -
/// ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
///
/// When writing with WriteFile or WriteConsole, characters are parsed for VT100 and similar control character sequences that
/// control cursor movement, color/font mode, and other operations that can also be performed via the existing Console APIs. For
/// more information, see Console Virtual Terminal Sequences.
///
///
/// -
/// DISABLE_NEWLINE_AUTO_RETURN 0x0008
///
/// When writing with WriteFile or WriteConsole, this adds an additional state to end-of-line wrapping that can delay the cursor
/// move and buffer scroll operations. Normally when ENABLE_WRAP_AT_EOL_OUTPUT is set and text reaches the end of the line, the
/// cursor will immediately move to the next line and the contents of the buffer will scroll up by one line. In contrast with this
/// flag set, the scroll operation and cursor move is delayed until the next character arrives. The written character will be
/// printed in the final position on the line and the cursor will remain above this character as if ENABLE_WRAP_AT_EOL_OUTPUT was
/// off, but the next printable character will be printed as if ENABLE_WRAP_AT_EOL_OUTPUT is on. No overwrite will occur.
/// Specifically, the cursor quickly advances down to the following line, a scroll is performed if necessary, the character is
/// printed, and the cursor advances one more position. The typical usage of this flag is intended in conjunction with setting
/// ENABLE_VIRTUAL_TERMINAL_PROCESSING to better emulate a terminal emulator where writing the final character on the screen (in the
/// bottom right corner) without triggering an immediate scroll is the desired behavior.
///
///
/// -
/// ENABLE_LVB_GRID_WORLDWIDE 0x0010
///
/// The APIs for writing character attributes including WriteConsoleOutput and WriteConsoleOutputAttribute allow the usage of flags
/// from character attributes to adjust the color of the foreground and background of text. Additionally, a range of DBCS flags was
/// specified with the COMMON_LVB prefix. Historically, these flags only functioned in DBCS code pages for Chinese, Japanese, and
/// Korean languages. With exception of the leading byte and trailing byte flags, the remaining flags describing line drawing and
/// reverse video (swap foreground and background colors) can be useful for other languages to emphasize portions of output. With
/// exception of the leading byte and trailing byte flags, the remaining flags describing line drawing and reverse video (swap
/// foreground and background colors) can be useful for other languages to emphasize portions of output. Setting this console mode
/// flag will allow these attributes to be used in every code page on every language. It is off by default to maintain compatibility
/// with known applications that have historically taken advantage of the console ignoring these flags on non-CJK machines to store
/// bits in these fields for their own purposes or by accident. Note that using the ENABLE_VIRTUAL_TERMINAL_PROCESSING mode can
/// result in LVB grid and reverse video flags being set while this flag is still off if the attached application requests
/// underlining or inverse video via Console Virtual Terminal Sequences.
///
///
///
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleMode( _In_ HANDLE hConsoleHandle, _In_ DWORD dwMode );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleMode(HFILE hConsoleHandle, CONSOLE_INPUT_MODE dwMode);
/// Sets the input mode of a console's input buffer or the output mode of a console screen buffer.
///
/// A handle to the console input buffer or a console screen buffer. The handle must have the GENERIC_READ access right. For
/// more information, see Console Buffer Security and Access Rights.
///
///
///
/// The input or output mode to be set. If the hConsoleHandle parameter is an input handle, the mode can be one or more of the
/// following values. When a console is created, all input modes except ENABLE_WINDOW_INPUT are enabled by default.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// ENABLE_ECHO_INPUT 0x0004
///
/// Characters read by the ReadFile or ReadConsole function are written to the active screen buffer as they are read. This mode can
/// be used only if the ENABLE_LINE_INPUT mode is also enabled.
///
///
/// -
/// ENABLE_EXTENDED_FLAGS 0x0080
/// Required to enable or disable extended flags. See ENABLE_INSERT_MODE and ENABLE_QUICK_EDIT_MODE.
///
/// -
/// ENABLE_INSERT_MODE 0x0020
///
/// When enabled, text entered in a console window will be inserted at the current cursor location and all text following that
/// location will not be overwritten. When disabled, all following text will be overwritten. To enable this mode, use . To disable
/// this mode, use ENABLE_EXTENDED_FLAGS without this flag.
///
///
/// -
/// ENABLE_LINE_INPUT 0x0002
///
/// The ReadFile or ReadConsole function returns only when a carriage return character is read. If this mode is disabled, the
/// functions return when one or more characters are available.
///
///
/// -
/// ENABLE_MOUSE_INPUT 0x0010
///
/// If the mouse pointer is within the borders of the console window and the window has the keyboard focus, mouse events generated
/// by mouse movement and button presses are placed in the input buffer. These events are discarded by ReadFile or ReadConsole, even
/// when this mode is enabled.
///
///
/// -
/// ENABLE_PROCESSED_INPUT 0x0001
///
/// CTRL+C is processed by the system and is not placed in the input buffer. If the input buffer is being read by ReadFile or
/// ReadConsole, other control keys are processed by the system and are not returned in the ReadFile or ReadConsole buffer. If the
/// ENABLE_LINE_INPUT mode is also enabled, backspace, carriage return, and line feed characters are handled by the system.
///
///
/// -
/// ENABLE_QUICK_EDIT_MODE 0x0040
///
/// This flag enables the user to use the mouse to select and edit text. To enable this mode, use . To disable this mode, use
/// ENABLE_EXTENDED_FLAGS without this flag.
///
///
/// -
/// ENABLE_WINDOW_INPUT 0x0008
///
/// User interactions that change the size of the console screen buffer are reported in the console's input buffer. Information
/// about these events can be read from the input buffer by applications using the ReadConsoleInput function, but not by those using
/// ReadFile or ReadConsole.
///
///
/// -
/// ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
///
/// Setting this flag directs the Virtual Terminal processing engine to convert user input received by the console window into
/// Console Virtual Terminal Sequences that can be retrieved by a supporting application through ReadFile or ReadConsole functions.
/// The typical usage of this flag is intended in conjunction with ENABLE_VIRTUAL_TERMINAL_PROCESSING on the output handle to
/// connect to an application that communicates exclusively via virtual terminal sequences.
///
///
///
///
///
/// If the hConsoleHandle parameter is a screen buffer handle, the mode can be one or more of the following values. When a screen
/// buffer is created, both output modes are enabled by default.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// ENABLE_PROCESSED_OUTPUT 0x0001
///
/// Characters written by the WriteFile or WriteConsole function or echoed by the ReadFile or ReadConsole function are examined for
/// ASCII control sequences and the correct action is performed. Backspace, tab, bell, carriage return, and line feed characters are processed.
///
///
/// -
/// ENABLE_WRAP_AT_EOL_OUTPUT 0x0002
///
/// When writing with WriteFile or WriteConsole or echoing with ReadFile or ReadConsole, the cursor moves to the beginning of the
/// next row when it reaches the end of the current row. This causes the rows displayed in the console window to scroll up
/// automatically when the cursor advances beyond the last row in the window. It also causes the contents of the console screen
/// buffer to scroll up (discarding the top row of the console screen
/// buffer) when the cursor advances beyond the last row in the console screen buffer. If this mode is disabled, the last character
/// in the row is overwritten with any subsequent characters.
///
///
/// -
/// ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
///
/// When writing with WriteFile or WriteConsole, characters are parsed for VT100 and similar control character sequences that
/// control cursor movement, color/font mode, and other operations that can also be performed via the existing Console APIs. For
/// more information, see Console Virtual Terminal Sequences.
///
///
/// -
/// DISABLE_NEWLINE_AUTO_RETURN 0x0008
///
/// When writing with WriteFile or WriteConsole, this adds an additional state to end-of-line wrapping that can delay the cursor
/// move and buffer scroll operations. Normally when ENABLE_WRAP_AT_EOL_OUTPUT is set and text reaches the end of the line, the
/// cursor will immediately move to the next line and the contents of the buffer will scroll up by one line. In contrast with this
/// flag set, the scroll operation and cursor move is delayed until the next character arrives. The written character will be
/// printed in the final position on the line and the cursor will remain above this character as if ENABLE_WRAP_AT_EOL_OUTPUT was
/// off, but the next printable character will be printed as if ENABLE_WRAP_AT_EOL_OUTPUT is on. No overwrite will occur.
/// Specifically, the cursor quickly advances down to the following line, a scroll is performed if necessary, the character is
/// printed, and the cursor advances one more position. The typical usage of this flag is intended in conjunction with setting
/// ENABLE_VIRTUAL_TERMINAL_PROCESSING to better emulate a terminal emulator where writing the final character on the screen (in the
/// bottom right corner) without triggering an immediate scroll is the desired behavior.
///
///
/// -
/// ENABLE_LVB_GRID_WORLDWIDE 0x0010
///
/// The APIs for writing character attributes including WriteConsoleOutput and WriteConsoleOutputAttribute allow the usage of flags
/// from character attributes to adjust the color of the foreground and background of text. Additionally, a range of DBCS flags was
/// specified with the COMMON_LVB prefix. Historically, these flags only functioned in DBCS code pages for Chinese, Japanese, and
/// Korean languages. With exception of the leading byte and trailing byte flags, the remaining flags describing line drawing and
/// reverse video (swap foreground and background colors) can be useful for other languages to emphasize portions of output. With
/// exception of the leading byte and trailing byte flags, the remaining flags describing line drawing and reverse video (swap
/// foreground and background colors) can be useful for other languages to emphasize portions of output. Setting this console mode
/// flag will allow these attributes to be used in every code page on every language. It is off by default to maintain compatibility
/// with known applications that have historically taken advantage of the console ignoring these flags on non-CJK machines to store
/// bits in these fields for their own purposes or by accident. Note that using the ENABLE_VIRTUAL_TERMINAL_PROCESSING mode can
/// result in LVB grid and reverse video flags being set while this flag is still off if the attached application requests
/// underlining or inverse video via Console Virtual Terminal Sequences.
///
///
///
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleMode( _In_ HANDLE hConsoleHandle, _In_ DWORD dwMode );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleMode(HFILE hConsoleHandle, CONSOLE_OUTPUT_MODE dwMode);
///
/// Sets the output code page used by the console associated with the calling process. A console uses its output code page to
/// translate the character values written by the various output functions into the images displayed in the console window.
///
/// The identifier of the code page to set. For more information, see Remarks.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleOutputCP( _In_ UINT wCodePageID );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleOutputCP(uint wCodePageID);
/// Sets extended information about the specified console screen buffer.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A CONSOLE_SCREEN_BUFFER_INFOEX structure that contains the console screen buffer information.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleScreenBufferInfoEx( _In_ HANDLE hConsoleOutput, _In_ PCONSOLE_SCREEN_BUFFER_INFOEX
// lpConsoleScreenBufferInfoEx );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern bool SetConsoleScreenBufferInfoEx(HFILE hConsoleOutput, in CONSOLE_SCREEN_BUFFER_INFOEX lpConsoleScreenBufferInfoEx);
/// Changes the size of the specified console screen buffer.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A COORD structure that specifies the new size of the console screen buffer, in character rows and columns. The specified
/// width and height cannot be less than the width and height of the console screen buffer's window. The specified dimensions also
/// cannot be less than the minimum size allowed by the system. This minimum depends on the current font size for the console
/// (selected by the user) and the SM_CXMIN and SM_CYMIN values returned by the GetSystemMetrics function.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleScreenBufferSize( _In_ HANDLE hConsoleOutput, _In_ COORD dwSize );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleScreenBufferSize(HFILE hConsoleOutput, COORD dwSize);
///
/// Sets the attributes of characters written to the console screen buffer by the WriteFile or WriteConsole function,
/// or echoed by the ReadFile or ReadConsole function. This function affects text written after the function call.
///
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
/// The character attributes.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleTextAttribute( _In_ HANDLE hConsoleOutput, _In_ WORD wAttributes );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleTextAttribute(HFILE hConsoleOutput, CHARACTER_ATTRIBUTE wAttributes);
/// Sets the title for the current console window.
///
/// The string to be displayed in the title bar of the console window. The total size must be less than 64K.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleTitle( _In_ LPCTSTR lpConsoleTitle );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleTitle(string lpConsoleTitle);
/// Sets the current size and position of a console screen buffer's window.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// If this parameter is TRUE, the coordinates specify the new upper-left and lower-right corners of the window. If it is
/// FALSE, the coordinates are relative to the current window-corner coordinates.
///
///
/// A pointer to a SMALL_RECT structure that specifies the new upper-left and lower-right corners of the window.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetConsoleWindowInfo( _In_ HANDLE hConsoleOutput, _In_ BOOL bAbsolute, _In_ const SMALL_RECT *lpConsoleWindow );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetConsoleWindowInfo(HFILE hConsoleOutput, [MarshalAs(UnmanagedType.Bool)] bool bAbsolute, in SMALL_RECT lpConsoleWindow);
/// Sets extended information about the current console font.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// If this parameter is TRUE, font information is set for the maximum window size. If this parameter is FALSE, font
/// information is set for the current window size.
///
/// A pointer to a CONSOLE_FONT_INFOEX structure that contains the font information.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetCurrentConsoleFontEx( _In_ HANDLE hConsoleOutput, _In_ BOOL bMaximumWindow, _In_ PCONSOLE_FONT_INFOEX
// lpConsoleCurrentFontEx );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
public static extern bool SetCurrentConsoleFontEx(HFILE hConsoleOutput, [MarshalAs(UnmanagedType.Bool)] bool bMaximumWindow, in CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);
/// Converts a to the color value within a enum.
/// The ConsoleColor value.
///
/// if set to true, the conversion represents a background color; otherwise it is a foreground color.
///
/// The equivalent CHARACTER_ATTRIBUTE value.
public static CHARACTER_ATTRIBUTE ToCharAttr(this ConsoleColor cc, bool backGround = false) => backGround ? (CHARACTER_ATTRIBUTE)((ushort)cc << 4) : (CHARACTER_ATTRIBUTE)cc;
/// Writes a character string to a console screen buffer beginning at the current cursor location.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a buffer that contains characters to be written to the console screen buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
///
/// The number of characters to be written. If the total size of the specified number of characters exceeds the available heap, the
/// function fails with ERROR_NOT_ENOUGH_MEMORY.
///
/// A pointer to a variable that receives the number of characters actually written.
/// Reserved; must be NULL.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI WriteConsole( _In_ HANDLE hConsoleOutput, _In_ const VOID *lpBuffer, _In_ DWORD nNumberOfCharsToWrite, _Out_ LPDWORD
// lpNumberOfCharsWritten, _Reserved_ LPVOID lpReserved );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WriteConsole(HFILE hConsoleOutput, [In] char[] lpBuffer, uint nNumberOfCharsToWrite, out uint lpNumberOfCharsWritten, IntPtr lpReserved = default);
/// Writes a character string to a console screen buffer beginning at the current cursor location.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to a buffer that contains characters to be written to the console screen buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
///
/// The number of characters to be written. If the total size of the specified number of characters exceeds the available heap, the
/// function fails with ERROR_NOT_ENOUGH_MEMORY.
///
/// A pointer to a variable that receives the number of characters actually written.
/// Reserved; must be NULL.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI WriteConsole( _In_ HANDLE hConsoleOutput, _In_ const VOID *lpBuffer, _In_ DWORD nNumberOfCharsToWrite, _Out_ LPDWORD
// lpNumberOfCharsWritten, _Reserved_ LPVOID lpReserved );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WriteConsole(HFILE hConsoleOutput, string lpBuffer, uint nNumberOfCharsToWrite, out uint lpNumberOfCharsWritten, IntPtr lpReserved = default);
/// Writes data directly to the console input buffer.
///
/// A handle to the console input buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// A pointer to an array of INPUT_RECORD structures that contain data to be written to the input buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
/// The number of input records to be written.
/// A pointer to a variable that receives the number of input records actually written.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI WriteConsoleInput( _In_ HANDLE hConsoleInput, _In_ const INPUT_RECORD *lpBuffer, _In_ DWORD nLength, _Out_ LPDWORD
// lpNumberOfEventsWritten );
[DllImport(Lib.Kernel32, SetLastError = true, EntryPoint = "WriteConsoleInputW", CharSet = CharSet.Unicode)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WriteConsoleInput(HFILE hConsoleInput, [In] INPUT_RECORD[] lpBuffer, uint nLength, out uint lpNumberOfEventsWritten);
///
/// Writes character and color attribute data to a specified rectangular block of character cells in a console screen buffer. The
/// data to be written is taken from a correspondingly sized rectangular block at a specified location in the source buffer.
///
///
/// A handle to the console screen buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
///
/// The data to be written to the console screen buffer. This pointer is treated as the origin of a two-dimensional array of
/// CHAR_INFO structures whose size is specified by the dwBufferSize parameter.
///
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
///
/// The size of the buffer pointed to by the lpBuffer parameter, in character cells. The X member of the COORD
/// structure is the number of columns; the Y member is the number of rows.
///
///
/// The coordinates of the upper-left cell in the buffer pointed to by the lpBuffer parameter. The X member of the
/// COORD structure is the column, and the Y member is the row.
///
///
/// A pointer to a SMALL_RECT structure. On input, the structure members specify the upper-left and lower-right coordinates
/// of the console screen buffer rectangle to write to. On output, the structure members specify the actual rectangle that was used.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI WriteConsoleOutput( _In_ HANDLE hConsoleOutput, _In_ const CHAR_INFO *lpBuffer, _In_ COORD dwBufferSize, _In_ COORD
// dwBufferCoord, _Inout_ PSMALL_RECT lpWriteRegion );
[DllImport(Lib.Kernel32, SetLastError = true, EntryPoint = "WriteConsoleOutputW", CharSet = CharSet.Unicode)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WriteConsoleOutput(HFILE hConsoleOutput, [In] CHAR_INFO[] lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpWriteRegion);
///
/// Copies a number of character attributes to consecutive cells of a console screen buffer, beginning at a specified location.
///
///
/// A handle to the console screen buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// The attributes to be used when writing to the console screen buffer. For more information, see Character Attributes.
///
///
/// The number of screen buffer character cells to which the attributes will be copied.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
///
/// A COORD structure that specifies the character coordinates of the first cell in the console screen buffer to which the
/// attributes will be written.
///
///
/// A pointer to a variable that receives the number of attributes actually written to the console screen buffer.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI WriteConsoleOutputAttribute( _In_ HANDLE hConsoleOutput, _In_ const WORD *lpAttribute, _In_ DWORD nLength, _In_ COORD
// dwWriteCoord, _Out_ LPDWORD lpNumberOfAttrsWritten );
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WriteConsoleOutputAttribute(HFILE hConsoleOutput, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] CHARACTER_ATTRIBUTE[] lpAttribute, uint nLength, COORD dwWriteCoord, out uint lpNumberOfAttrsWritten);
/// Copies a number of characters to consecutive cells of a console screen buffer, beginning at a specified location.
///
/// A handle to the console screen buffer. The handle must have the GENERIC_WRITE access right. For more information, see
/// Console Buffer Security and Access Rights.
///
///
/// The characters to be written to the console screen buffer.
///
/// The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the
/// buffer will depend on heap usage.
///
///
/// The number of characters to be written.
///
/// A COORD structure that specifies the character coordinates of the first cell in the console screen buffer to which
/// characters will be written.
///
/// A pointer to a variable that receives the number of characters actually written.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI WriteConsoleOutputCharacter( _In_ HANDLE hConsoleOutput, _In_ LPCTSTR lpCharacter, _In_ DWORD nLength, _In_ COORD
// dwWriteCoord, _Out_ LPDWORD lpNumberOfCharsWritten );
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Wincon.h", MSDNShortId = "")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WriteConsoleOutputCharacter(HFILE hConsoleOutput, string lpCharacter, uint nLength, COORD dwWriteCoord, out uint lpNumberOfCharsWritten);
///
/// Specifies a Unicode or ANSI character and its attributes. This structure is used by console functions to read from and write to
/// a console screen buffer.
///
// typedef struct _CHAR_INFO { union { WCHAR UnicodeChar; CHAR AsciiChar; } Char; WORD Attributes; } CHAR_INFO, *PCHAR_INFO;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Unicode)]
public struct CHAR_INFO
{
/// Translated Unicode character.
public char Char;
///
/// The character attributes. This member can be zero or any combination of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// FOREGROUND_BLUE 0x0001
/// Text color contains blue.
///
/// -
/// FOREGROUND_GREEN 0x0002
/// Text color contains green.
///
/// -
/// FOREGROUND_RED 0x0004
/// Text color contains red.
///
/// -
/// FOREGROUND_INTENSITY 0x0008
/// Text color is intensified.
///
/// -
/// BACKGROUND_BLUE 0x0010
/// Background color contains blue.
///
/// -
/// BACKGROUND_GREEN 0x0020
/// Background color contains green.
///
/// -
/// BACKGROUND_RED 0x0040
/// Background color contains red.
///
/// -
/// BACKGROUND_INTENSITY 0x0080
/// Background color is intensified.
///
/// -
/// COMMON_LVB_LEADING_BYTE 0x0100
/// Leading byte.
///
/// -
/// COMMON_LVB_TRAILING_BYTE 0x0200
/// Trailing byte.
///
/// -
/// COMMON_LVB_GRID_HORIZONTAL 0x0400
/// Top horizontal
///
/// -
/// COMMON_LVB_GRID_LVERTICAL 0x0800
/// Left vertical.
///
/// -
/// COMMON_LVB_GRID_RVERTICAL 0x1000
/// Right vertical.
///
/// -
/// COMMON_LVB_REVERSE_VIDEO 0x4000
/// Reverse foreground and background attribute.
///
/// -
/// COMMON_LVB_UNDERSCORE 0x8000
/// Underscore.
///
///
///
///
public CHARACTER_ATTRIBUTE Attributes;
/// Initializes a new instance of the struct.
/// A Unicode character.
/// The character attributes.
public CHAR_INFO(char @char, CHARACTER_ATTRIBUTE attr = 0)
{
Char = @char;
Attributes = attr;
}
}
/// Contains information about the console cursor.
// typedef struct _CONSOLE_CURSOR_INFO { DWORD dwSize; BOOL bVisible; } CONSOLE_CURSOR_INFO, *PCONSOLE_CURSOR_INFO;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_CURSOR_INFO
{
///
/// The percentage of the character cell that is filled by the cursor. This value is between 1 and 100. The cursor appearance
/// varies, ranging from completely filling the cell to showing up as a horizontal line at the bottom of the cell.
///
public uint dwSize;
/// The visibility of the cursor. If the cursor is visible, this member is TRUE.
[MarshalAs(UnmanagedType.Bool)]
public bool bVisible;
}
/// Contains information for a console font.
// typedef struct _CONSOLE_FONT_INFO { DWORD nFont; COORD dwFontSize; } CONSOLE_FONT_INFO, *PCONSOLE_FONT_INFO;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_FONT_INFO
{
/// The index of the font in the system's console font table.
public uint nFont;
///
/// A COORD structure that contains the width and height of each character in the font, in logical units. The X
/// member contains the width, while the Y member contains the height.
///
public COORD dwFontSize;
}
/// Contains extended information for a console font.
// typedef struct _CONSOLE_FONT_INFOEX { ULONG cbSize; DWORD nFont; COORD dwFontSize; UINT FontFamily; UINT FontWeight; WCHAR
// FaceName[LF_FACESIZE]; } CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CONSOLE_FONT_INFOEX
{
/// The size of this structure, in bytes.
public uint cbSize;
/// The index of the font in the system's console font table.
public uint nFont;
///
/// A COORD structure that contains the width and height of each character in the font, in logical units. The X
/// member contains the width, while the Y member contains the height.
///
public COORD dwFontSize;
///
/// The font pitch and family. For information about the possible values for this member, see the description of the
/// tmPitchAndFamily member of the TEXTMETRIC structure.
///
public uint FontFamily;
///
/// The font weight. The weight can range from 100 to 1000, in multiples of 100. For example, the normal weight is 400, while
/// 700 is bold.
///
public uint FontWeight;
/// The name of the typeface (such as Courier or Arial).
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string FaceName;
/// Gets an empty structure value with the field initialized to the correct value.
public static readonly CONSOLE_FONT_INFOEX Default = new CONSOLE_FONT_INFOEX { cbSize = (uint)Marshal.SizeOf(typeof(CONSOLE_FONT_INFOEX)) };
}
/// Contains information about the console history.
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_HISTORY_INFO
{
/// The size of the structure, in bytes. Set this member to sizeof(CONSOLE_HISTORY_INFO).
public uint cbSize;
/// The number of commands kept in each history buffer.
public uint HistoryBufferSize;
/// The number of history buffers kept for this console process.
public uint NumberOfHistoryBuffers;
///
/// This parameter can be zero or HISTORY_NO_DUP_FLAG (0x1) to indicate duplicate entries will not be stored in the history buffer.
///
public uint dwFlags;
/// Gets an empty structure value with the field initialized to the correct value.
public static readonly CONSOLE_HISTORY_INFO Default = new CONSOLE_HISTORY_INFO { cbSize = (uint)Marshal.SizeOf(typeof(CONSOLE_HISTORY_INFO)) };
}
/// Contains information for a console read operation.
// https://docs.microsoft.com/en-us/windows/console/console-readconsole-control typedef struct _CONSOLE_READCONSOLE_CONTROL { ULONG
// nLength; ULONG nInitialChars; ULONG dwCtrlWakeupMask; ULONG dwControlKeyState; } CONSOLE_READCONSOLE_CONTROL, *PCONSOLE_READCONSOLE_CONTROL;
[PInvokeData("ConsoleApi.h", MSDNShortId = "6a8451a6-d692-43af-88c4-972c4dc5e07c")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CONSOLE_READCONSOLE_CONTROL
{
/// The size of the structure. Set this member to sizeof(CONSOLE_READCONSOLE_CONTROL).
public uint nLength;
///
/// The number of characters to skip (and thus preserve) before writing newly read input in the buffer passed to the ReadConsole
/// function. This value must be less than the nNumberOfCharsToRead parameter of the ReadConsole function.
///
public uint nInitialChars;
/// A user-defined control character used to signal that the read is complete.
public uint dwCtrlWakeupMask;
/// The state of the control keys.
public CONTROL_KEY_STATE dwControlKeyState;
/// Gets an empty structure value with the field initialized to the correct value.
public static readonly CONSOLE_READCONSOLE_CONTROL Default = new CONSOLE_READCONSOLE_CONTROL { nLength = (uint)Marshal.SizeOf(typeof(CONSOLE_READCONSOLE_CONTROL)) };
}
/// Contains information about a console screen buffer.
// typedef struct _CONSOLE_SCREEN_BUFFER_INFO { COORD dwSize; COORD dwCursorPosition; WORD wAttributes; SMALL_RECT srWindow; COORD
// dwMaximumWindowSize; } CONSOLE_SCREEN_BUFFER_INFO;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_SCREEN_BUFFER_INFO
{
/// A COORD structure that contains the size of the console screen buffer, in character columns and rows.
public COORD dwSize;
/// A COORD structure that contains the column and row coordinates of the cursor in the console screen buffer.
public COORD dwCursorPosition;
///
/// The attributes of the characters written to a screen buffer by the WriteFile and WriteConsole functions, or
/// echoed to a screen buffer by the ReadFile and ReadConsole functions. For more information, see Character Attributes.
///
public CHARACTER_ATTRIBUTE wAttributes;
///
/// A SMALL_RECT structure that contains the console screen buffer coordinates of the upper-left and lower-right corners
/// of the display window.
///
public SMALL_RECT srWindow;
///
/// A COORD structure that contains the maximum size of the console window, in character columns and rows, given the
/// current screen buffer size and font and the screen size.
///
public COORD dwMaximumWindowSize;
}
/// Contains extended information about a console screen buffer.
// typedef struct _CONSOLE_SCREEN_BUFFER_INFOEX { ULONG cbSize; COORD dwSize; COORD dwCursorPosition; WORD wAttributes; SMALL_RECT
// srWindow; COORD dwMaximumWindowSize; WORD wPopupAttributes; BOOL bFullscreenSupported; COLORREF ColorTable[16]; }
// CONSOLE_SCREEN_BUFFER_INFOEX, *PCONSOLE_SCREEN_BUFFER_INFOEX;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_SCREEN_BUFFER_INFOEX
{
/// The size of this structure, in bytes.
public uint cbSize;
/// A COORD structure that contains the size of the console screen buffer, in character columns and rows.
public COORD dwSize;
/// A COORD structure that contains the column and row coordinates of the cursor in the console screen buffer.
public COORD dwCursorPosition;
///
/// The attributes of the characters written to a screen buffer by the WriteFile and WriteConsole functions, or
/// echoed to a screen buffer by the ReadFile and ReadConsole functions. For more information, see Character Attributes.
///
public CHARACTER_ATTRIBUTE wAttributes;
///
/// A SMALL_RECT structure that contains the console screen buffer coordinates of the upper-left and lower-right corners
/// of the display window.
///
public SMALL_RECT srWindow;
///
/// A COORD structure that contains the maximum size of the console window, in character columns and rows, given the
/// current screen buffer size and font and the screen size.
///
public COORD dwMaximumWindowSize;
/// The fill attribute for console pop-ups.
public CHARACTER_ATTRIBUTE wPopupAttributes;
/// If this member is TRUE, full-screen mode is supported; otherwise, it is not.
[MarshalAsAttribute(UnmanagedType.Bool)]
public bool bFullscreenSupported;
/// An array of COLORREF values that describe the console's color settings.
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public COLORREF[] ColorTable;
/// Gets an empty structure value with the field initialized to the correct value.
public static readonly CONSOLE_SCREEN_BUFFER_INFOEX Default = new CONSOLE_SCREEN_BUFFER_INFOEX { cbSize = (uint)Marshal.SizeOf(typeof(CONSOLE_SCREEN_BUFFER_INFOEX)) };
}
/// Contains information for a console selection.
// https://docs.microsoft.com/en-us/windows/console/console-selection-info-str
// typedef struct _CONSOLE_SELECTION_INFO { DWORD dwFlags; COORD dwSelectionAnchor; SMALL_RECT srSelection; } CONSOLE_SELECTION_INFO, *PCONSOLE_SELECTION_INFO;
[PInvokeData("ConsoleApi3.h")]
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_SELECTION_INFO
{
///
/// The selection indicator. This member can be one or more of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// CONSOLE_MOUSE_DOWN 0x0008
/// Mouse is down
///
/// -
/// CONSOLE_MOUSE_SELECTION 0x0004
/// Selecting with the mouse
///
/// -
/// CONSOLE_NO_SELECTION 0x0000
/// No selection
///
/// -
/// CONSOLE_SELECTION_IN_PROGRESS 0x0001
/// Selection has begun
///
/// -
/// CONSOLE_SELECTION_NOT_EMPTY 0x0002
/// Selection rectangle is not empty
///
///
///
///
public CONSOLE_SELECTION dwFlags;
/// A COORD structure that specifies the selection anchor, in characters.
public COORD dwSelectionAnchor;
/// A SMALL_RECT structure that specifies the selection rectangle.
public SMALL_RECT srSelection;
}
///
/// Defines the coordinates of a character cell in a console screen buffer. The origin of the coordinate system (0,0) is at the top,
/// left cell of the buffer.
///
// typedef struct _COORD { SHORT X; SHORT Y; } COORD, *PCOORD;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
/// The horizontal coordinate or column value. The units depend on the function call.
public short X;
/// The vertical coordinate or row value. The units depend on the function call.
public short Y;
/// Initializes a new instance of the struct.
/// The horizontal coordinate or column value.
/// The vertical coordinate or row value.
public COORD(int x, int y)
{
X = (short)x;
Y = (short)y;
}
/// Converts to string.
/// A that represents this instance.
public override string ToString() => $"X={X},Y={Y}";
/// Represents an empty instance of COORD with both X and Y values set to 0.
public static readonly COORD Empty = default(COORD);
}
///
/// Describes a focus event in a console INPUT_RECORD structure. These events are used internally and should be ignored.
///
// typedef struct _FOCUS_EVENT_RECORD { BOOL bSetFocus; } FOCUS_EVENT_RECORD;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct FOCUS_EVENT_RECORD
{
/// Reserved.
[MarshalAs(UnmanagedType.Bool)]
public bool bSetFocus;
}
/// Provides a handle to a psuedo console.
[StructLayout(LayoutKind.Sequential)]
public struct HPCON : IHandle
{
private IntPtr handle;
/// Initializes a new instance of the struct.
/// An object that represents the pre-existing handle to use.
public HPCON(IntPtr preexistingHandle) => handle = preexistingHandle;
/// Returns an invalid handle by instantiating a object with .
public static HPCON NULL => new HPCON(IntPtr.Zero);
/// Gets a value indicating whether this instance is a null handle.
public bool IsNull => handle == IntPtr.Zero;
/// Performs an explicit conversion from to .
/// The handle.
/// The result of the conversion.
public static explicit operator IntPtr(HPCON h) => h.handle;
/// Performs an implicit conversion from to .
/// The pointer to a handle.
/// The result of the conversion.
public static implicit operator HPCON(IntPtr h) => new HPCON(h);
/// Implements the operator !=.
/// The first handle.
/// The second handle.
/// The result of the operator.
public static bool operator !=(HPCON h1, HPCON h2) => !(h1 == h2);
/// Implements the operator ==.
/// The first handle.
/// The second handle.
/// The result of the operator.
public static bool operator ==(HPCON h1, HPCON h2) => h1.Equals(h2);
///
public override bool Equals(object obj) => obj is HPCON h && handle == h.handle;
///
public override int GetHashCode() => handle.GetHashCode();
///
public IntPtr DangerousGetHandle() => handle;
}
///
/// Describes an input event in the console input buffer. These records can be read from the input buffer by using the
/// ReadConsoleInput or PeekConsoleInput function, or written to the input buffer by using the
/// WriteConsoleInput function.
///
// typedef struct _INPUT_RECORD { WORD EventType; union { KEY_EVENT_RECORD KeyEvent; MOUSE_EVENT_RECORD MouseEvent;
// WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent; MENU_EVENT_RECORD MenuEvent; FOCUS_EVENT_RECORD FocusEvent; } Event; } INPUT_RECORD;
[PInvokeData("Wincon.h", MSDNShortId = "a46ba7fd-097a-455d-96ac-13aa01e11dc1")]
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct INPUT_RECORD
{
///
/// A handle to the type of input event and the event record stored in the Event member.
/// This member can be one of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// FOCUS_EVENT 0x0010
/// The Event member contains a FOCUS_EVENT_RECORD structure. These events are used internally and should be ignored.
///
/// -
/// KEY_EVENT 0x0001
/// The Event member contains a KEY_EVENT_RECORD structure with information about a keyboard event.
///
/// -
/// MENU_EVENT 0x0008
/// The Event member contains a MENU_EVENT_RECORD structure. These events are used internally and should be ignored.
///
/// -
/// MOUSE_EVENT 0x0002
/// The Event member contains a MOUSE_EVENT_RECORD structure with information about a mouse movement or button press event.
///
/// -
/// WINDOW_BUFFER_SIZE_EVENT 0x0004
///
/// The Event member contains a WINDOW_BUFFER_SIZE_RECORD structure with information about the new size of the console screen buffer.
///
///
///
///
///
[FieldOffset(0)]
public EVENT_TYPE EventType;
/// The event information. The format of this member depends on the event type specified by the EventType member.
[FieldOffset(4)]
public INPUT_RECORD_EVENT Event;
///
[StructLayout(LayoutKind.Explicit)]
public struct INPUT_RECORD_EVENT
{
/// The key event
[FieldOffset(0)]
public KEY_EVENT_RECORD KeyEvent;
/// The mouse event
[FieldOffset(0)]
public MOUSE_EVENT_RECORD MouseEvent;
/// The window buffer size event
[FieldOffset(0)]
public WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
/// The menu event
[FieldOffset(0)]
public MENU_EVENT_RECORD MenuEvent;
/// The focus event
[FieldOffset(0)]
public FOCUS_EVENT_RECORD FocusEvent;
}
/// Creates a new with key event information.
///
/// If the key is pressed, this member is TRUE. Otherwise, this member is FALSE (the key is released).
///
/// A virtual-key code that identifies the given key in a device-independent manner.
///
/// The virtual scan code of the given key that represents the device-dependent value generated by the keyboard hardware.
///
/// Translated Unicode character.
/// The state of the control keys.
/// The repeat count, which indicates that a key is being held down.
/// A value.
public static INPUT_RECORD CreateKeyEventRecord(bool keyDown, ushort virtualKeyCode, ushort virtualScanCode, char unicodeChar,
CONTROL_KEY_STATE ctrlKeys = CONTROL_KEY_STATE.NONE, ushort repeatCount = 1) =>
new INPUT_RECORD
{
EventType = EVENT_TYPE.KEY_EVENT,
Event = new INPUT_RECORD_EVENT
{
KeyEvent = new KEY_EVENT_RECORD
{
bKeyDown = keyDown,
wRepeatCount = repeatCount,
wVirtualKeyCode = virtualKeyCode,
wVirtualScanCode = virtualScanCode,
uChar = unicodeChar,
dwControlKeyState = ctrlKeys
}
}
};
}
/// Describes a keyboard input event in a console INPUT_RECORD structure.
// typedef struct _KEY_EVENT_RECORD { BOOL bKeyDown; WORD wRepeatCount; WORD wVirtualKeyCode; WORD wVirtualScanCode; union { WCHAR
// UnicodeChar; CHAR AsciiChar; } uChar; DWORD dwControlKeyState; } KEY_EVENT_RECORD;
[PInvokeData("Wincon.h", MSDNShortId = "b3fed86b-84ef-48e4-97e1-cb3d65f714a7")]
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct KEY_EVENT_RECORD
{
/// If the key is pressed, this member is TRUE. Otherwise, this member is FALSE (the key is released).
[FieldOffset(0), MarshalAs(UnmanagedType.Bool)]
public bool bKeyDown;
///
/// The repeat count, which indicates that a key is being held down. For example, when a key is held down, you might get five
/// events with this member equal to 1, one event with this member equal to 5, or multiple events with this member greater than
/// or equal to 1.
///
[FieldOffset(4), MarshalAs(UnmanagedType.U2)]
public ushort wRepeatCount;
/// A virtual-key code that identifies the given key in a device-independent manner.
[FieldOffset(6), MarshalAs(UnmanagedType.U2)]
public ushort wVirtualKeyCode;
///
/// The virtual scan code of the given key that represents the device-dependent value generated by the keyboard hardware.
///
[FieldOffset(8), MarshalAs(UnmanagedType.U2)]
public ushort wVirtualScanCode;
/// Translated Unicode character.
[FieldOffset(10)]
public char uChar;
///
/// The state of the control keys. This member can be one or more of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// CAPSLOCK_ON 0x0080
/// The CAPS LOCK light is on.
///
/// -
/// ENHANCED_KEY 0x0100
/// The key is enhanced.
///
/// -
/// LEFT_ALT_PRESSED 0x0002
/// The left ALT key is pressed.
///
/// -
/// LEFT_CTRL_PRESSED 0x0008
/// The left CTRL key is pressed.
///
/// -
/// NUMLOCK_ON 0x0020
/// The NUM LOCK light is on.
///
/// -
/// RIGHT_ALT_PRESSED 0x0001
/// The right ALT key is pressed.
///
/// -
/// RIGHT_CTRL_PRESSED 0x0004
/// The right CTRL key is pressed.
///
/// -
/// SCROLLLOCK_ON 0x0040
/// The SCROLL LOCK light is on.
///
/// -
/// SHIFT_PRESSED 0x0010
/// The SHIFT key is pressed.
///
///
///
///
[FieldOffset(12), MarshalAs(UnmanagedType.U4)]
public CONTROL_KEY_STATE dwControlKeyState;
}
///
/// Describes a menu event in a console INPUT_RECORD structure. These events are used internally and should be ignored.
///
// typedef struct _MENU_EVENT_RECORD { UINT dwCommandId; } MENU_EVENT_RECORD, *PMENU_EVENT_RECORD;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct MENU_EVENT_RECORD
{
/// Reserved.
public uint dwCommandId;
}
/// Describes a mouse input event in a console INPUT_RECORD structure.
// typedef struct _MOUSE_EVENT_RECORD { COORD dwMousePosition; DWORD dwButtonState; DWORD dwControlKeyState; DWORD dwEventFlags; } MOUSE_EVENT_RECORD;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct MOUSE_EVENT_RECORD
{
///
/// A COORD structure that contains the location of the cursor, in terms of the console screen buffer's character-cell coordinates.
///
public COORD dwMousePosition;
///
///
/// The status of the mouse buttons. The least significant bit corresponds to the leftmost mouse button. The next least
/// significant bit corresponds to the rightmost mouse button. The next bit indicates the next-to-leftmost mouse button. The
/// bits then correspond left to right to the mouse buttons. A bit is 1 if the button was pressed.
///
/// The following constants are defined for the first five mouse buttons.
///
///
///
/// Value
/// Meaning
///
/// -
/// FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
/// The leftmost mouse button.
///
/// -
/// FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
/// The second button from the left.
///
/// -
/// FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
/// The third button from the left.
///
/// -
/// FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
/// The fourth button from the left.
///
/// -
/// RIGHTMOST_BUTTON_PRESSED 0x0002
/// The rightmost mouse button.
///
///
///
///
public MOUSE_BUTTON_STATE dwButtonState;
///
/// The state of the control keys. This member can be one or more of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// CAPSLOCK_ON 0x0080
/// The CAPS LOCK light is on.
///
/// -
/// ENHANCED_KEY 0x0100
/// The key is enhanced.
///
/// -
/// LEFT_ALT_PRESSED 0x0002
/// The left ALT key is pressed.
///
/// -
/// LEFT_CTRL_PRESSED 0x0008
/// The left CTRL key is pressed.
///
/// -
/// NUMLOCK_ON 0x0020
/// The NUM LOCK light is on.
///
/// -
/// RIGHT_ALT_PRESSED 0x0001
/// The right ALT key is pressed.
///
/// -
/// RIGHT_CTRL_PRESSED 0x0004
/// The right CTRL key is pressed.
///
/// -
/// SCROLLLOCK_ON 0x0040
/// The SCROLL LOCK light is on.
///
/// -
/// SHIFT_PRESSED 0x0010
/// The SHIFT key is pressed.
///
///
///
///
public CONTROL_KEY_STATE dwControlKeyState;
///
///
/// The type of mouse event. If this value is zero, it indicates a mouse button being pressed or released. Otherwise, this
/// member is one of the following values.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// DOUBLE_CLICK 0x0002
///
/// The second click (button press) of a double-click occurred. The first click is returned as a regular button-press event.
///
///
/// -
/// MOUSE_HWHEELED 0x0008
///
/// The horizontal mouse wheel was moved. If the high word of the dwButtonState member contains a positive value, the wheel was
/// rotated to the right. Otherwise, the wheel was rotated to the left.
///
///
/// -
/// MOUSE_MOVED 0x0001
/// A change in mouse position occurred.
///
/// -
/// MOUSE_WHEELED 0x0004
///
/// The vertical mouse wheel was moved. If the high word of the dwButtonState member contains a positive value, the wheel was
/// rotated forward, away from the user. Otherwise, the wheel was rotated backward, toward the user.
///
///
///
///
///
public MOUSE_EVENT_FLAG dwEventFlags;
}
/// Defines the coordinates of the upper left and lower right corners of a rectangle.
// typedef struct _SMALL_RECT { SHORT Left; SHORT Top; SHORT Right; SHORT Bottom; } SMALL_RECT;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct SMALL_RECT
{
/// The x-coordinate of the upper left corner of the rectangle.
public short Left;
/// The y-coordinate of the upper left corner of the rectangle.
public short Top;
/// The x-coordinate of the lower right corner of the rectangle.
public short Right;
/// The y-coordinate of the lower right corner of the rectangle.
public short Bottom;
/// Initializes a new instance of the struct.
/// The x-coordinate of the upper left corner of the rectangle.
/// The y-coordinate of the upper left corner of the rectangle.
/// The x-coordinate of the lower right corner of the rectangle.
/// The y-coordinate of the lower right corner of the rectangle.
public SMALL_RECT(short left, short top, short right, short bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
/// Converts to string.
/// A that represents this instance.
public override string ToString() => $"L={Left},R={Right},T={Top},B={Bottom}";
}
/// Describes a change in the size of the console screen buffer.
// typedef struct _WINDOW_BUFFER_SIZE_RECORD { COORD dwSize; } WINDOW_BUFFER_SIZE_RECORD;
[PInvokeData("Wincon.h", MSDNShortId = "")]
[StructLayout(LayoutKind.Sequential)]
public struct WINDOW_BUFFER_SIZE_RECORD
{
///
/// A COORD structure that contains the size of the console screen buffer, in character cell columns and rows.
///
public COORD dwSize;
}
/// Provides a for that is disposed using .
public class SafeHPCON : SafeHANDLE
{
/// Initializes a new instance of the class and assigns an existing handle.
/// An object that represents the pre-existing handle to use.
///
/// to reliably release the handle during the finalization phase; otherwise, (not recommended).
///
public SafeHPCON(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { }
/// Initializes a new instance of the class.
private SafeHPCON() : base() { }
/// Performs an implicit conversion from to .
/// The safe handle instance.
/// The result of the conversion.
public static implicit operator HPCON(SafeHPCON h) => h.handle;
///
protected override bool InternalReleaseHandle() { ClosePseudoConsole(handle); return true; }
}
}
}