9

On Win7 having localized UI, error_code::message() returns a non-English message. As far as I see (in Boost 1.54, for system_error_category), the above function boils down to the following WinAPI call:

DWORD retval = ::FormatMessageA( 
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM | 
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    ev,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPSTR) &lpMsgBuf,
    0,
    NULL 
);

How to get the above FormatMessage to return an English message? I tried to set the locale, both with std functions and with SetThreadLocale - it didn't help.

Update: Just a clarification: essentially, my question is how to "override" programmatically the user default language and why setting locale is not enough.

Igor R.
  • 14,716
  • 2
  • 49
  • 83

2 Answers2

4

Been searching all over the internet for solution, and finally found this. Basically, you should call SetThreadUILanguage in your main/WinMain.

Andrew Kravchuk
  • 356
  • 1
  • 5
  • 17
2

At a guess, you'll need to specify English for dwLanguageId instead of the default language. E.g.:

MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT)

or, if you want specifically US English:

MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)

Note that this will fail if the message in the specified language is not present. So you may want to handle ERROR_RESOURCE_LANG_NOT_FOUND and try calling it again with dwLanguageId=0.

For more info, see MSDN.

Igor Skochinsky
  • 24,629
  • 2
  • 72
  • 109
  • 1
    As I mentioned, it's not my code, but Boost.System that calls `FormatMessage`. – Igor R. Jul 04 '13 at 12:01
  • Well, write your own function then. It's not difficult. – Igor Skochinsky Jul 04 '13 at 12:05
  • Patching Boost is an option, but I'd prefer a more clean solution. Actually, the question is whether it's possible to override (LANG_NEUTRAL,SUBLANG_DEFAULT), i.e. "User default language" (http://msdn.microsoft.com/en-us/library/windows/desktop/dd373908(v=vs.85).aspx). Shouldn't thread locale or global locale affect it? – Igor R. Jul 04 '13 at 14:11