Skip to content

Instantly share code, notes, and snippets.

@mattn
Created August 17, 2019 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/2a70cfc0de2aebf30bd67aba51b74366 to your computer and use it in GitHub Desktop.
Save mattn/2a70cfc0de2aebf30bd67aba51b74366 to your computer and use it in GitHub Desktop.
WriteConsoleOutputW is broken on Windows 10
#include <windows.h>
#include <stdio.h>
int
main(int argc, char* argv[]) {
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR_INFO ci[3] = {};
COORD buffer_size = { 3, 1 };
COORD start_coord = { 0, 0 };
SMALL_RECT sr = { 0, 0, 80, 25 };
SHORT white = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
ci[0].Char.UnicodeChar = L'あ';
ci[0].Attributes = white;
ci[1].Char.UnicodeChar = L'い';
ci[1].Attributes = white;
ci[2].Char.UnicodeChar = L'う';
ci[2].Attributes = white;
WriteConsoleOutputW(h, (CHAR_INFO*)ci, buffer_size, start_coord, &sr);
return 0;
}
@mattn
Copy link
Author

mattn commented Dec 14, 2020

This code above is broken. Below should work correctly.

#include <windows.h>
#include <stdio.h>

int
main(int argc, char* argv[]) {
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	CHAR_INFO ci[6] = {};
	COORD buffer_size = { 6, 1 };
	COORD start_coord = { 0, 0 };
	SMALL_RECT sr = { 0, 0, 80, 25 };
	SHORT white = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;

	ci[0].Char.UnicodeChar = L'あ';
	ci[0].Attributes = white | COMMON_LVB_LEADING_BYTE ;
	ci[1].Char.UnicodeChar = L'あ';
	ci[1].Attributes = white | COMMON_LVB_TRAILING_BYTE ;
	ci[2].Char.UnicodeChar = L'い';
	ci[2].Attributes = white | COMMON_LVB_LEADING_BYTE ;
	ci[3].Char.UnicodeChar = L'い';
	ci[3].Attributes = white | COMMON_LVB_TRAILING_BYTE ;
	ci[4].Char.UnicodeChar = L'う';
	ci[4].Attributes = white | COMMON_LVB_LEADING_BYTE ;
	ci[5].Char.UnicodeChar = L'う';
	ci[5].Attributes = white | COMMON_LVB_TRAILING_BYTE ;
	WriteConsoleOutputW(h, (CHAR_INFO*)ci, buffer_size, start_coord, &sr);	
	return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment