Kernel32MultiByteToWideChar(CodePage, MultiByteCharConversionType, String, Int32, StringBuilder, Int32) Method

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

Caution: Using the MultiByteToWideChar(CodePage, MultiByteCharConversionType, Byte, Int32, Char, Int32) 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 multiByteStr equals the number of bytes in the string, while the size of the output buffer indicated by wideCharStr equals the number of characters.

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

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", SetLastError = true)]
public static int MultiByteToWideChar(
	CodePage codePage,
	MultiByteCharConversionType flags,
	string multiByteStr,
	int multiByteStrSize,
	StringBuilder wideCharStr,
	int wideCharStrSize
)

Parameters

codePage  CodePage
Code page to use in performing the conversion.
flags  MultiByteCharConversionType
Flags indicating the conversion type.
multiByteStr  String
Pointer to the character string to convert.
multiByteStrSize  Int32
Size, in bytes, of the string indicated by the multiByteStr parameter.

Alternatively, this parameter can be set to -1 if the string is null-terminated. Note that, if multiByteStrSize is 0, the function fails.

If this parameter is -1, the function processes the entire input string, including the terminating null character. Therefore, the resulting Unicode 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 bytes. If the provided size does not include a terminating null character, the resulting Unicode string is not null-terminated, and the returned length does not include this character.

wideCharStr  StringBuilder
Pointer to a buffer that receives the converted string.
wideCharStrSize  Int32
Size, in characters, of the buffer indicated by wideCharStr.

If this value is 0, the function returns the required buffer size, in characters, including any terminating null character, and makes no use of the wideCharStr buffer.

Return Value

Int32
Returns the number of characters written to the buffer indicated by wideCharStr if successful.

If the function succeeds and wideCharStrSize is 0, the return value is the required size, in characters, for the buffer indicated by wideCharStr.

The function returns 0 if it does not succeed.

Remarks

See Also