I've got a Unicode string containing four Japanese characters and I'm using WideCharToMultiByte to convert it to a multi-byte string specifying the Shift-JIS codepage of 932. In order to get the size of the required buffer I'm calling WideCharToMultiByte first with the cbMultiByte parameter set to 0. This is returning 9 as expected, but then when I actually call WideCharToMultiByte again to do the conversion it's returning the number of bytes written as 13. An example is below, I'm currently hard coding my buffer size to 100:
BSTR value = SysAllocString(L"日経先物");
char *buffer = new char[100];
int sizeRequired = WideCharToMultiByte(932, 0, value, -1, NULL, 0, NULL, NULL);
// sizeRequired is 9 as expected
int bytesWritten = WideCharToMultiByte(932, 0, value, sizeRequired, buffer, 100, NULL, NULL);
// bytesWritten is 13
buffer[8] contains the string terminator \0 as expected. buffer[9-12] contains byte 63.
So if I set the size of my buffer to be sizeRequired it's too small and the second call to WideCharToMultiByte fails. Does anyone know why an extra 4 bytes are written each with a byte value of 63?
You are passing the wrong arguments to WideCharToMultiByte in your second call (the required size of the destination as the length of the source). You need to change
int bytesWritten = WideCharToMultiByte(932, 0, value, sizeRequired, buffer, 100,
NULL, NULL);
to
int bytesWritten = WideCharToMultiByte(932, 0, value, -1, buffer, sizeRequired,
NULL, NULL);