WriteConsoleOutput function
Important
This document describes console platform functionality that is no longer a part of our ecosystem roadmap. We do not recommend using this content in new products, but we will continue to support existing usages for the indefinite future. Our preferred modern solution focuses on virtual terminal sequences for maximum compatibility in cross-platform scenarios. You can find more information about this design decision in our classic console vs. virtual terminal document.
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.
Syntax
BOOL WINAPI WriteConsoleOutput(
_In_ HANDLE hConsoleOutput,
_In_ const CHAR_INFO *lpBuffer,
_In_ COORD dwBufferSize,
_In_ COORD dwBufferCoord,
_Inout_ PSMALL_RECT lpWriteRegion
);
Parameters
hConsoleOutput [in]
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.
lpBuffer [in]
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.
dwBufferSize [in]
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.
dwBufferCoord [in]
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.
lpWriteRegion [in, out]
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.
Return value
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.
Remarks
WriteConsoleOutput treats the source buffer and the destination screen buffer as two-dimensional arrays (columns and rows of character cells). The rectangle pointed to by the lpWriteRegion parameter specifies the size and location of the block to be written to in the console screen buffer. A rectangle of the same size is located with its upper-left cell at the coordinates of the dwBufferCoord parameter in the lpBuffer array. Data from the cells that are in the intersection of this rectangle and the source buffer rectangle (whose dimensions are specified by the dwBufferSize parameter) is written to the destination rectangle.
Cells in the destination rectangle whose corresponding source location are outside the boundaries of the source buffer rectangle are left unaffected by the write operation. In other words, these are the cells for which no data is available to be written.
Before WriteConsoleOutput returns, it sets the members of lpWriteRegion to the actual screen buffer rectangle affected by the write operation. This rectangle reflects the cells in the destination rectangle for which there existed a corresponding cell in the source buffer, because WriteConsoleOutput clips the dimensions of the destination rectangle to the boundaries of the console screen buffer.
If the rectangle specified by lpWriteRegion lies completely outside the boundaries of the console screen buffer, or if the corresponding rectangle is positioned completely outside the boundaries of the source buffer, no data is written. In this case, the function returns with the members of the structure pointed to by the lpWriteRegion parameter set such that the Right member is less than the Left, or the Bottom member is less than the Top. To determine the size of the console screen buffer, use the GetConsoleScreenBufferInfo function.
WriteConsoleOutput has no effect on the cursor position.
This function uses either Unicode characters or 8-bit characters from the console's current code page. The console's code page defaults initially to the system's OEM code page. To change the console's code page, use the SetConsoleCP or SetConsoleOutputCP functions. Legacy consumers may also use the chcp or mode con cp select= commands, but it is not recommended for new development.
Tip
This API has a virtual terminal equivalent in the text formatting and cursor positioning sequences. Move the cursor to the location to insert, apply the formatting desired, and write out the text. Virtual terminal sequences are recommended for all new and ongoing development.
Examples
For an example, see Reading and Writing Blocks of Characters and Attributes.
Requirements
Minimum supported client | Windows 2000 Professional [desktop apps only] |
Minimum supported server | Windows 2000 Server [desktop apps only] |
Header | ConsoleApi2.h (via WinCon.h, include Windows.h) |
Library | Kernel32.lib |
DLL | Kernel32.dll |
Unicode and ANSI names | WriteConsoleOutputW (Unicode) and WriteConsoleOutputA (ANSI) |