Internationalization and localization tools


Windows Safe String Function

HRESULT StringCchCopy(LPTSTR pszDest, size_t cchDest, LPCTSTR pszSrc);

HRESULT StringCchCopyA(LPSTR pszDest, size_t cchDest, LPCSTR pszSrc);

HRESULT StringCchCopyW(LPWSTR pszDest, size_t cchDest, LPCWSTR pszSrc);

HRESULT StringCchCopyEx(LPTSTR pszDest, size_t cchDest, LPCTSTR pszSrc, LPTSTR *ppszDestEnd, size_t *pcchRemaining, DWORD dwFlags);

HRESULT StringCchCopyExA(LPSTR pszDest, size_t cchDest, LPCSTR pszSrc, LPSTR *ppszDestEnd, size_t *pcchRemaining, DWORD dwFlags);

HRESULT StringCchCopyExW(LPWSTR pszDest, size_t cchDest, LPCWSTR pszSrc, LPWSTR *ppszDestEnd, size_t *pcchRemaining, DWORD dwFlags);

Internationalization (I18n) Function Overview

The StringCchCopy function is a replacement for locale-sensitive string copy functions. It copies pszStr to pszDest, using the character size of the buffer stored in cchDest to ensure that the destination string buffer is not overrun. In the event of an error, a negative HRESULT value is returned, indicating the error.

StringCchCopyEx adds advanced functionality: It returns a pointer to the end of the destination string as well as the number of characters left unused in that string, and uses dwFlags for additional control.

The narrow versions of these functions, StringCchCopyA and StringCchCopyExA, pass in single-byte or multibyte strings and lengths that refer to the number of single-byte characters.

The wide versions of these functions, StringCchCopyW and StringCchCopyExW, pass in wide-character strings 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 cchDest is set to the character size of the destination 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. These are shown as the first choices 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