WriteConsoleW, wprintf and Unicode

后端 未结 2 627
情歌与酒
情歌与酒 2021-01-03 08:57
AllocConsole();
consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleW(consoleHandle, L\"qweąęėšų\\n\", 9, NULL, NULL);
_wfreopen(L\"CONOUT$\", L\"w\", stdou         


        
2条回答
  •  再見小時候
    2021-01-03 09:06

    The stdout stream can be redirected and therefore always operates in 8-bit mode. The Unicode string you pass to wprintf() gets converted from utf-16 to the 8-bit code page that's selected for the console. By default that's the olden 437 OEM code page. That's where the buck stops, that code page doesn't support the character.

    You'll need to switch to another 8-bit code page, one that does support that character. A good choice is 65001, the code page for utf-8. Fix:

     SetConsoleOutputCP(CP_UTF8);
    

    Or use SetConsoleCP() if you want stdin to use utf-8 as well.

提交回复
热议问题