【发布时间】:2016-10-12 10:37:10
【问题描述】:
many examples 有人建议使用类似的技巧来获取 Unicode 控制台输出:
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.
这似乎工作得很好。
MSDN 文档 only ever mentions(至少据我所知)您 should use WriteConsoleW 用于控制台输出,WriteFile 用于重定向输出。 (可以通过GetConsoleMode的返回值等方法检测句柄是否为控制台句柄)。
是否微软官方支持使用SetConsoleOutputCP(CP_UT8)将Unicode文本输出到控制台并重定向输出?如果是,它记录在哪里?
我认为 UTF-8 多字节代码页只能用于 WideCharToMultiByte 和 MultiByteToWideChar 函数?
【问题讨论】:
-
不管怎样,文档中列出的注册表项在我的系统上不包含 65001 (
CP_UTF8)。从WriteFile is apparently confused by UTF-8 console output 开始,您将看到程序/库行为不端。此外,还有一些 API(如IsDBCSLeadByte)本质上与 UTF-8 不兼容。
标签: delphi winapi unicode console delphi-xe7