Internationalization and localization tools


Locale-Sensitive Windows C++ Function

int CompareString(LCID Locale, DWORD dwCmpFlags, LPCTSTR lpString1, int cchCount1, LPCTSTR lpString2, int cchCount2);

int CompareString(LCID Locale, DWORD dwCmpFlags, LPCSTR lpString1, int cchCount1, LPCSTR lpString2, int cchCount2);

int CompareString(LCID Locale, DWORD dwCmpFlags, LPCWSTR lpString1, int cchCount1, LPCWSTR lpString2, int cchCount2);

Internationalization (I18n) Function Overview

The CompareString function compares lpString1 and lpString2, based upon the specified locale and the flags passed in dwCmpFlags, and returns:

CSTR_LESS_THAN if lpString1 is less in lexical value than lpString2;
CSTR_EQUAL if lpString1 is equal in lexical value to lpString2;
CSTR_GREATER_THAN if lpString1 is less in lexical value than lpString2.

0 is returned if CompareString fails, and a global error value can be retrieved by calling GetLastError. The error return will be either ERROR_INVALID_FLAGS or ERROR_INVALID_PARAMETER.

cchCount1 and cchCount2 are the size in TCHARs of lpString1 and lpString2, respectively. If either of these arguments are a negative value, then all of the characters in the null-terminated string are compared.

CompareStringA is the narrow version of the function, passing in single or multibyte strings and length values in bytes.

CompareStringW is the wide version of the function, passing in wide-character strings and the length values in wide characters (WCHARs).

I18n Issues

Use the appropriate version of the function as required for internationalization support, noting the following:

Formulate the correct locale to pass into CompareString. Although there are two predefined locale constants that may be used: LOCALE_SYSTEM_DEFAULT (the system's default locale returned by GetSystemDefaultLCID) and LOCALE_USER_DEFAULT (the current user's default locale returned by GetUserDefaultLCID), neither of these will work in an internationalized application where the locale is independent of the user's system settings.

Ensure that cchCount1 and cchCount2 are correct for the chosen platform; bytes for the narrow version and WCHARs for the wide version. See Locale-Sensitive Length Functions for a discussion on multibyte and wide character sizes.

Recommended Replacements*

When possible, use the Generic version of the function, rather than the narrow or wide versions, and let the Windows #define UNICODE switch determine which version of the function will be called.

*If you're already using the recommended function, see I18n Issues for other reasons why Globalyzer is detecting the function.

Locale-Sensitive Windows C++ Functions

 

Lingoport internationalization and localization services and software