How to get boost::system::error_code::message in English?

1.6k Views Asked by At

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.

2

There are 2 best solutions below

0
On

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

3
On

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.