There are many examples were people suggest using tricks similar to this to get Unicode console output:
begin
OldConsoleOutputCP := GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
try
// Might also use WriteConsoleA, but this has drawbacks with output redirection
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), Utf8Bytes, ...);
finally
// We better restore the output CP that was in use before our program started!
SetConsoleOutputCP(OldConsoleOutputCP);
end;
end.
This seems to work pretty well.
The MSDN documentation only ever mentions (at least to my knowledge) that you should use WriteConsoleW
for console output and WriteFile
for redirected output. (You can detect whether the handle is a console handle via the return value of GetConsoleMode
and similar methods).
Is it officially supported by Microsoft to use SetConsoleOutputCP(CP_UT8)
to output Unicode text to the console and redirected output? If yes where is it documented?
I thought that the UTF-8 multi byte codepage should only ever be used for the WideCharToMultiByte
and MultiByteToWideChar
functions?