Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 356
Igor R. Avatar asked Jul 04 '13 08:07

Igor R.


2 Answers

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

like image 116
Andrew Kravchuk Avatar answered Nov 06 '22 10:11

Andrew Kravchuk


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.

like image 2
Igor Skochinsky Avatar answered Nov 06 '22 10:11

Igor Skochinsky