Kernel32WideCharToMultiByte(UInt32, WideCharConversionType, String, Int32, StringBuilder, Int32, String, Boolean) Method

Maps a UTF-16 (wide character) string to a new character string. The new character string is not necessarily from a multibyte character set.

Caution: Using the WideCharToMultiByte(CodePage, WideCharConversionType, String, Int32, StringBuilder, Int32, String, Boolean) function incorrectly can compromise the security of your application. Calling this function can easily cause a buffer overrun because the size of the input buffer indicated by wideCharStr equals the number of characters in the Unicode string, while the size of the output buffer indicated by multiByteStr equals the number of bytes.

To avoid a buffer overrun, your application must specify a buffer size appropriate for the data type the buffer receives.

Data converted from UTF-16 to non-Unicode encodings is subject to data loss, because a code page might not be able to represent every character used in the specific Unicode data.

Definition

Namespace: DevCase.Win32.NativeMethods
Assembly: DevCase.net48 (in DevCase.net48.dll) Version: 6.0.0.0 (6.0)
XMLNS for XAML: Not mapped to an xmlns.
[DllImportAttribute("Kernel32.dll", ExactSpelling = true, SetLastError = true)]
public static int WideCharToMultiByte(
	uint codePage,
	WideCharConversionType flags,
	string wideCharStr,
	int wideCharStrSize,
	StringBuilder multiByteStr,
	int multiByteStrSize,
	string defaultChar,
	out bool refUsedDefaultChar
)

Parameters

codePage  UInt32
Code page to use in performing the conversion.

This parameter can be set to the value of any code page that is installed or available in the operating system.

flags  WideCharConversionType
Flags indicating the conversion type.
wideCharStr  String
Pointer to the Unicode string to convert.
wideCharStrSize  Int32
Size, in characters, of the string indicated by wideCharStr.

Alternatively, this parameter can be set to -1 if the string is null-terminated.

If wideCharStrSize is set to 0, the function fails.

If this parameter is -1, the function processes the entire input string, including the terminating null character. Therefore, the resulting character string has a terminating null character, and the length returned by the function includes this character.

If this parameter is set to a positive integer, the function processes exactly the specified number of characters.

If the provided size does not include a terminating null character, the resulting character string is not null-terminated, and the returned length does not include this character.

multiByteStr  StringBuilder
Pointer to a buffer that receives the converted string.
multiByteStrSize  Int32
Size, in bytes, of the buffer indicated by multiByteStr.

If this parameter is set to 0, the function returns the required buffer size for multiByteStr and makes no use of the output parameter itself.

defaultChar  String
Pointer to the character to use if a character cannot be represented in the specified code page.

The application sets this parameter to if the function is to use a system default value.

To obtain the system default character, the application can call the GetCPInfo or GetCPInfoEx function.

If you specify the UTF7 or UTF8 value for codePage parameter, this parameter must be set to . Otherwise, the function fails with ERROR_INVALID_PARAMETER.

refUsedDefaultChar  Boolean
Pointer to a flag that indicates if the function has used a default character in the conversion.

The flag is set to if one or more characters in the source string cannot be represented in the specified code page. Otherwise, the flag is set to .

This parameter can be et to . If you specify the UTF7 or UTF8 value for codePage parameter, this parameter must be set to . Otherwise, the function fails with ERROR_INVALID_PARAMETER.

Return Value

Int32
If successful, returns the number of bytes written to the buffer pointed to by multiByteStr.

If the function succeeds and multiByteStrSize is 0, the return value is the required size, in bytes, for the buffer indicated by multiByteStr.

The function returns 0 (zero) if it does not succeed.

Remarks

See Also