The mbstowcs_s function converts a string of multibyte characters pointed to by mbstr into wide characters stored in the buffer pointed to by wcstr. The conversion will continue for each character until one of these conditions is met:
A multibyte null character is encountered
An invalid multibyte character is encountered
The number of wide characters stored in the wcstr buffer equals count.
The destination string is always null-terminated (even in the case of an error).
If count is the special value _TRUNCATE, then mbstowcs_s converts as much of the string as will fit into the destination buffer, while still leaving room for a null terminator.
If mbstowcs_s successfully converts the source string, it puts the size in wide characters of the converted string, including the null terminator, into *pReturnValue (provided pReturnValue is not NULL). This occurs even if the wcstr argument is NULL and provides a way to determine the required buffer size. Note that if wcstr is NULL, count is ignored, and sizeInWords must be 0.
If mbstowcs_s encounters an invalid multibyte character, it puts 0 in *pReturnValue, sets the destination buffer to an empty string, sets errno to EILSEQ, and returns EILSEQ.
If the sequences pointed to by mbstr and wcstr overlap, the behavior of mbstowcs_s is undefined.
Security Note: |
|---|
Ensure that wcstr and mbstr do not overlap, and that count correctly reflects the number of multibyte characters to convert. |
mbstowcs_s uses the current locale for any locale-dependent behavior; _mbstowcs_s_l is identical except that it uses the locale passed in instead. For more information, see Locale.
In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads.