Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FormatMessage c++ for getting english only messages

Tags:

locale

winapi

I am trying to use format message to obtain English only values

MSDN specifies the following

DWORD WINAPI FormatMessage(
  _In_      DWORD dwFlags,
  _In_opt_  LPCVOID lpSource,
  _In_      DWORD dwMessageId,
  _In_      DWORD dwLanguageId,
  _Out_     LPTSTR lpBuffer,
  _In_      DWORD nSize,
  _In_opt_  va_list *Arguments
);

For dwLanguageId parameter,

The language identifier for the requested message. This parameter is ignored if dwFlags includes FORMAT_MESSAGE_FROM_STRING.

If you pass a specific LANGID in this parameter, FormatMessage will return a message for that LANGID only. If the function cannot find a message for that LANGID, it sets Last-Error to ERROR_RESOURCE_LANG_NOT_FOUND. If you pass in zero, FormatMessage looks for a message for LANGIDs in the following order:

  1. Language neutral
  2. Thread LANGID, based on the thread's locale value
  3. User default LANGID, based on the user's default locale value
  4. System default LANGID, based on the system default locale value
  5. US English

If FormatMessage does not locate a message for any of the preceding LANGIDs, it returns any language message string that is present. If that fails, it returns ERROR_RESOURCE_LANG_NOT_FOUND.

Would this mean that, if is pass value 5, it will return messages in English?

Can you please clarify this?

like image 549
Nida Sahar Avatar asked Oct 03 '12 19:10

Nida Sahar


1 Answers

The number 5 in the documentation indicates that U.S. English is the fifth language FormatMessage will try to use when you pass zero as the parameter value, after it has tried and failed the previous four options. That has nothing to do with passing the value 5 for that function parameter.

To request an English message, pass in the LANGID value for U.S. English, which you can get with MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US). The FormatMessage documentation links to documentation on language identifiers, which in turn links to a page of language identifier constants and strings.

like image 81
Rob Kennedy Avatar answered Sep 19 '22 08:09

Rob Kennedy