@DaveK said:
Oh, and to answer your question:
@poizan42 said:
They are still byte oriented FIFOs (or maybe word (as in 16-bit ints) oriented FIFOs - what happens if you write an odd number of bytes to a wide character console handle anyways?).
That's not even possible, as you'd know if you weren't posting uninformed speculation on a subject you have no knowledge of.
I thank you for your insightful and educating comment sir. To answer my own question:
SetConsoleOutputCP refuses to set the codepage to 1200/1201 (UTF-16LE/UTF-16BE), so we can't WriteFile UTF-16 strings to the console.
WriteConsoleOutput wants the number of characters and not bytes to write, so you can't write incomplete wide chars (more precisely you can't write incomplete UTF-16 code units - you can still write half of a surrogate pair).
There are still other multibyte encodings left. Writing incomplete or wrong data with WriteFile results in replacement characters in their stead. Example:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
LPCWSTR utf16le = L"æøå";
char utf16be[] = { 0xE6, 0, 0xF8, 0, 0xE5, 0 };
char utf8[] = { 0xC3, 0xA6, 0xC3, 0xB8, 0xC3, 0xA5 };
UINT cp = GetConsoleOutputCP();
printf("UTF-16LE:\n");
if (!SetConsoleOutputCP(1200)) //UTF-16LE
printf("Set UTF-16LE failed.");
else
WriteFile(out, utf16le, 6, NULL, NULL);
SetConsoleOutputCP(cp);
printf("\nUTF-16BE:\n");
if (!SetConsoleOutputCP(1201)) //UTF-16BE
printf("Set UTF-16BE failed.");
else
WriteFile(out, utf16be, 6, NULL, NULL);
SetConsoleOutputCP(cp);
printf("\nUTF-8:\n");
if (!SetConsoleOutputCP(65001)) //UTF-8
printf("Set UTF-8 failed.");
else
WriteFile(out, utf8, 6, NULL, NULL);
SetConsoleOutputCP(cp);
printf("\nUTF-8 partial string:\n");
if (!SetConsoleOutputCP(65001)) //UTF-8
printf("Set UTF-8 failed.");
else
WriteFile(out, utf8, 5, NULL, NULL);
SetConsoleOutputCP(cp);
printf("\nfin\n");
return 0;
}
Output:
UTF-16LE:
Set UTF-16LE failed.
UTF-16BE:
Set UTF-16BE failed.
UTF-8:
æøå
UTF-8 partial string:
æø�
fin