Internationalization and localization tools

Windows Safe String Function

HRESULT StringCchLength(LPTSTR psz, size_t cchMax, size_t *pcch);

HRESULT StringCchLengthA(LPSTR psz, size_t cchMax, size_t *pcch);

HRESULT StringCchLengthW(LPWSTR psz, size_t cchMax, size_t *pcch);

Internationalization (I18n) Function Overview

The StringCchLength function is a replacement for locale-sensitive string length functions. It calculates the character length of the string stored in psz, and stores the result in pcch, ensuring that the character size of the string is not larger than cchMax. In the event of an error, a negative HRESULT value is returned, indicating the error.

The narrow version of the function, StringCchLengthA, passes in a single-byte or multibyte string and lengths that refer to the number of single-byte characters.

The wide version of the function, StringCchLengthW, passes in a wide-character string and lengths that refer to the number of wide (wchar_t) characters.

I18n Issues

Use the appropriate version of the function as required for internationalization support, and ensure that cchMax is set to the character size of the string buffer: on Windows MBCS, the size of a character is one byte; on Windows Unicode, it's two bytes. 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. This is shown as the first choice in the following table:

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

Windows Safe String Functions


Lingoport internationalization and localization services and software