WideCharToMultiByte

This function maps a wide-character string to a new character string. The new character string is not necessarily from a multibyte character set.

At a Glance

Header file: Winnls.h
Windows CE versions: 1.01 and later

Syntax

int WideCharToMultiByte(UINT CodePage, DWORD dwFlags,
LPCWSTR
lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr,
int
cchMultiByte, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar);

Parameters

CodePage

[in] Specifies the code page used to perform the conversion. This parameter can be given the value of any code page that is installed or available in the system. You can also specify one of the following values:

Value Description
CP_ACP ANSI code page
CP_MACCP Not supported
CP_OEMCP OEM code page
CP_SYMBOL Not supported
CP_THREAD_ACP Not supported

dwFlags

[in] Specifies the handling of unmapped characters. The function performs more quickly when none of these flags is set. The following flag constants are defined:

Value Description
WC_COMPOSITECHECK Convert composite characters to precomposed characters.
WC_DISCARDNS Discard nonspacing characters during conversion.
WC_SEPCHARS Generate separate characters during conversion. This is the default conversion behavior.
WC_DEFAULTCHAR Replace exceptions with the default character during conversion.

When WC_COMPOSITECHECK is specified, the function converts composite characters to precomposed characters. A composite character consists of a base character and a nonspacing character, each having different character values. A precomposed character has a single character value for a base/nonspacing character combination. In the character è, the e is the base character, and the accent grave mark is the nonspacing character.

When an application specifies WC_COMPOSITECHECK, it can use the last three flags in this list (WC_DISCARDNS, WC_SEPCHARS, and WC_DEFAULTCHAR) to customize the conversion to precomposed characters. These flags determine the function’s behavior when there is no precomposed mapping for a base/nonspace character combination in a wide-character string. These last three flags can only be used if the WC_COMPOSITECHECK flag is set.

The function’s default behavior is to generate separate characters (WC_SEPCHARS) for unmapped composite characters.

lpWideCharStr

[in] Pointer to the wide-character string to be converted.

cchWideChar

[in] Specifies the number of Unicode (16-bit) characters in the string pointed to by the lpWideCharStr parameter. If this value is –1, the string is assumed to be null-terminated and the length is calculated automatically.

lpMultiByteStr

[out] Pointer to the buffer to receive the translated string.

cbMultiByte

[in] Specifies the size in bytes of the buffer pointed to by the lpMultiByteStr parameter. If this value is zero, the function returns the number of bytes required for the buffer. (In this case, the lpMultiByteStr buffer is not used.)

lpDefaultChar

[in] Pointer to the character used if a wide character cannot be represented in the specified code page. If this parameter is NULL, a system default value is used. The function is faster when both lpDefaultChar and lpUsedDefaultChar are NULL.

lpUsedDefaultChar

[in/out] Pointer to a flag that indicates whether a default character was used. The flag is set to TRUE if one or more wide characters in the source string cannot be represented in the specified code page. Otherwise, the flag is set to FALSE. This parameter may be NULL. The function is faster when both lpDefaultChar and lpUsedDefaultChar are NULL.

Return Values

If cbMultiByte is nonzero, the number of bytes written to the buffer pointed to by lpMultiByteStr indicates success. If cbMultiByte is zero, the required size, in bytes, for a buffer that can receive the translated string indicates success. Zero indicates failure. To get extended error information, call GetLastError. Possible values for GetLastError include the following:

Remarks

The lpMultiByteStr and lpWideCharStr pointers must not be the same. If they are the same, the function fails, and GetLastError returns ERROR_INVALID_PARAMETER.

An application can use the lpDefaultChar parameter to change the default character used for the conversion.

As noted earlier, the WideCharToMultiByte function operates most efficiently when both lpDefaultChar and lpUsedDefaultChar are NULL. The following table shows the behavior of WideCharToMultiByte for the four combinations of lpDefaultChar and lpUsedDefaultChar :

lpDefaultChar lpUsedDefaultChar Result
NULL NULL No default checking. This is the most efficient, quick way to use this function.
non-NULL NULL Uses the specified default character, but does not set lpUsedDefaultChar.
NULL non-NULL Uses the system default character and sets lpUsedDefaultChar if necessary.
non-NULL non-NULL Uses the specified default character and sets lpUsedDefaultChar if necessary.

See Also

MultiByteToWideChar