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

懵懂的女人 提交于 2019-12-22 04:43:12

问题


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.


回答1:


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




回答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.



来源:https://stackoverflow.com/questions/17465526/how-to-get-boostsystemerror-codemessage-in-english

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!