When mbstr contains characters not supported by the current process code page, mbstowcs_s does not return an error and put garbage characters in wcstr.
setlocale(LC_CTYPE, ".1252"); //set the process to use a locale with English code page
//you can also try not setting the locale. The default process LC_CTYPE locale is C
//which means 7-bit ASCII.
char* mbTestStr = "Test. 真的."; //this is a 9 character string with 2 Chinese characters.
errno_t error = mbstowcs_s(charCount, wcStr, 50, mbStr, -1);
After the call, no error is returned, charCount becomes 12, and wcStr contains "Test. ÕæµÄ." It seems charCount is the actual byte count in mbStr. The two Chinese characters each takes two bytes in mbStr.
The function should fail to convert the Chinese characters and return an error because the code page does not support Chinese characters.
If I set the locale to ".936" (936 is a code page for simplified Chinese). No error is returned, charCount becomes 10, and wcStr contains "Test. 真的.". Everything is correct.
_mbstowcs_s_l has the same problem if you give it a locale that does not support all the characters in mbstr.