Console unicode output

Eric Lippert discussed how Windows Script Host output unicode in console for NT based OSes here.

Of course, every console based applications have the same problem. This includes gacutil.

In 1.0 and 1.1, gacutil translate everything to ANSI, then use printf to show the result. This is certainly not unicode friendly as not every unicode can be translated to ANSI code page.

In Whidbey, we do the following:

Our approach is, always call WideCharToMultiByte with GetConsoleOutputCP(), then call WriteFile to output the result. This way it does not matter if the output is console, or file, or pipe, and what OS it is running on, as long as the console is about to show the characters.

Dr. International has the follow code sample in his book "Developing International Software" version 2:

wchar_t*   szwOut ;
DWORD      dwBytesWritten;
DWORD      fdwMode;
HANDLE     outHandle = GetStdHandle(STD_OUTPUT_HANDLE);
// ThreadLocale adjustment, resource loading, etc. is skipped
if( (GetFileType(outHandle) & FILE_TYPE_CHAR) &&
GetConsoleMode( outHandle, &fdwMode) )
    WriteConsoleW( outHandle, szwOut, wcslen(szwOut),
        &dwBytesWritten, 0);
    int nOutputCP = GetConsoleOutputCP();
    int charCount = WideCharToMultiByte(nOutputCP, 0, szwOut, -1, 0,
                    0, 0, 0);
    char* szaStr = (char*) malloc(charCount);
    WideCharToMultiByte( nOutputCP, 0, szwOut, -1, szaStr, charCount,
    0, 0);
    WriteFile(outHandle, szaStr, charCount-1, &dwBytesWritten, 0);

Note WriteFile writes charCount-1 bytes to filter out the NULL terminator.

Comments (4)

  1. Mike says:

    So when will windows have a true unicode console?

  2. Windows console is a true unicode console since NT4. You need the special treatment only if the standard output is redirected, or you care about Win9x.

Skip to main content