BSTR
is not the same as WCHAR[]
. BSTR
values are prefixed with their length, as well as null-terminated.
If you re dealing with in-process objects that are written in C or C++, you ll usually get away with this, because the C/C++ code will probably assume that your BSTR is a null-terminated wide character string.
If, on the other hand, you re dealing with out-of-process/cross-machine objects, the proxy/stub marshalling code will assume that you really did pass a BSTR, and will expect to find a length field (it needs this to know how much data to marshal). This will go horribly wrong.
In short: if something expects a BSTR, call SysAllocString (or CComBSTR, or CString::AllocSysString).