Writes a character string to a console screen buffer beginning at the current cursor location.
Syntax
BOOL WINAPI WriteConsole(
__in HANDLE hConsoleOutput,
__in const VOID *lpBuffer,
__in DWORD nNumberOfCharsToWrite,
__out LPDWORD lpNumberOfCharsWritten,
__reserved LPVOID lpReserved
);
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]
-
A pointer to a buffer that contains characters to be written to the console screen buffer. The total size must be less than 64K.
- nNumberOfCharsToWrite [in]
-
The number of TCHARs to write. If the total size of the specified number of characters exceeds 64 KB, the function fails with ERROR_NOT_ENOUGH_MEMORY.
- lpNumberOfCharsWritten [out]
-
A pointer to a variable that receives the number of TCHARs actually written.
- lpReserved
-
Reserved; must be NULL.
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
The WriteConsole function writes characters to the console screen buffer at the current cursor position. The cursor position advances as characters are written. The
SetConsoleCursorPosition function sets the current cursor position.
Characters are written using the foreground and background color attributes associated with the console screen buffer. The
SetConsoleTextAttribute function changes these colors. To determine the current color attributes and the current cursor position, use
GetConsoleScreenBufferInfo.
All of the input modes that affect the behavior of the WriteFile function have the same effect on
WriteConsole. To retrieve and set the output modes of a console screen buffer, use the
GetConsoleMode and
SetConsoleMode functions.
This function uses either Unicode characters or ANSI 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, or use the chcp or mode con cp select= commands.
Both WriteConsole and WriteFile can be used for console I/O. While WriteConsole supports writing Unicode characters to a console screen buffer, WriteFile does not. However, WriteConsole fails if it is used with a standard handle that is redirected to a file. If an application handles multilingual output that can be redirected, determine whether the output handle is a console handle (one method is to call the GetConsoleMode function and check whether it succeeds). If the handle is a console handle, call WriteConsole; otherwise, the output is redirected and you should call WideCharToMultiByte to convert the text to the current code page and WriteFile to perform the I/O.
Although an application can use
WriteConsole in ANSI mode to write ANSI characters, consoles do not support ANSI escape sequences. However, some functions provide equivalent functionality. For more information,
see SetCursorPos,
SetConsoleTextAttribute, and
GetConsoleCursorInfo.
Requirements
| Minimum supported client | Windows 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Header | Wincon.h (include Windows.h) |
| Library | Kernel32.lib |
| DLL | Kernel32.dll |
| Unicode and ANSI names | WriteConsoleW (Unicode) and WriteConsoleA (ANSI) |
See Also
- Console Functions
- GetConsoleCursorInfo
- GetConsoleMode
- GetConsoleScreenBufferInfo
- Input and Output Methods
- ReadConsole
- SetConsoleCP
- SetConsoleCursorPosition
- SetConsoleMode
- SetConsoleOutputCP
- SetConsoleTextAttribute
- SetCursorPos
- WriteFile
Send comments about this topic to Microsoft
Build date: 10/8/2009