Welcome to MSDN Blogs Sign in | Join | Help

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);
}
else
{
    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);
    free(szaStr);
}

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

Published Wednesday, February 25, 2004 2:16 PM by junfeng
Filed under:

Comments

# re: Console unicode output

Tuesday, June 15, 2004 8:31 PM by Mike
So when will windows have a true unicode console?

# re: Console unicode output

Wednesday, June 16, 2004 5:41 AM by Junfeng Zhang
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.

# Making console output Unicode-friendly.

Tuesday, July 26, 2005 1:47 AM by The Emission Locus

# Making console output Unicode-friendly.

Tuesday, July 26, 2005 1:48 AM by The Emission Locus
New Comments to this post are disabled
 
Page view tracker