Error code 31 means "Use Unicode, please!"

by Michael S. Kaplan, published on 2011/03/30 07:01 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2011/03/30/10147538.aspx


The question came in a message to a few of the distribution lists I follow, a few months ago:

Hi,

Our LOC team is hitting an issue where WriteConsole fails with error code 31. The user locale in which this happens is German (the OS  Windows Server 2k8). Replacing WriteConsole with WriteFile(stdout) does not fail, but prints garbage.
I tried to repro the problem on another machine, by changing the locale to German, but did not hit this issue.

What does this error indicate? There seems to be very little information on the net about Unicode programming with windows consoles.

Regular readers might know what is going on here.

Though the point about the lack of information is valid (ignoring this Blog, of course!).

It turns out the problem was happening with a Japanese system locale on that German machine, which should give the clues for everyone else!

Now when you get down to it, any time the default system OEM code page ends up being

there are going to be some illegal byte combinations.

And as it turns out, the WriteConsoleA function can have trouble if some of the text you send to it is interpreted as if it is one of these code pages and converted, if in fact it in some other code page.

If ever there was a time to use WriteConsoleW and to just use Unicode, this would be it - when a mismatch between application language and console code page can leave one with either corrupt text or a nothing but an error code.


Random832 on 30 Mar 2011 8:53 AM:

Why is it error 31 [ERROR_GENERAL_FAILURE] instead of error 582 [ERROR_ILLEGAL_CHARACTER]?

Michael S. Kaplan on 31 Mar 2011 3:55 PM:

Not a bad idea, though it may be a generic failure handler that wasn't communicating more specific info, which no one wanted to change....

Definitely changing it now would be complicated.


go to newer or older post, or back to index or month or day