February 27, 2007

wprintf/wcout and unicode characters in VS2005

Starting with VC2005, the CRT now supports the output of unicode characters in the console. Before this was only possible with WriteConsoleW because the CRT had always translated the character into the current codepage and the wrote it via WriteFile.
See also my previous post:
wprintf/wcout has no UNICODE support

In VC2005, MS added support for writing native UNICODE characters to the console. But this feature is not enabled by default. You need to enable this feature via

_setmode(_fileno(stdout), _O_U16TEXT);

Internaly the CRT also uses the WriteConsoleW function.
The complete example the looks like:

  #define UNICODE
  #define _UNICODE
  #include <tchar.h>
  #include <stdio.h>
  #include <io.h>
  #include <fcntl.h>
  int _tmain()
  {
    _setmode(_fileno(stdout), _O_U16TEXT);
    _tprintf(_T("A\x0424") _T("C\n"));
    return 0;
  }

Posted 9 months, 3 days ago on February 27, 2007
The trackback url for this post is http://blog.kalmbachnet.de/bblog/trackback.php/98/

Comments have now been turned off for this post