How to marshall utf-8 bytestring via COM variant?
See the question and my original answer on StackOverflowThe problem was explained in the comments. As for the solution, because you're using std
(there are plenty other solutions), I suggest you use the widen
function defined in this answer on SO: Is this code safe using wstring with MultiByteToWideChar? and change the code to:
CComVariant result = widen(json).c_str();
Let's check under the debugger. Before:
After:
Now, the VARIANT (or it's contained BSTR) is fine.
Note if you need a byte string equivalent from this VARIANT or from a BSTR (do you really?), don't convert it back with a broken code like this: std::string(CStringA(varJson))
, again, use a reverse equivalent of widen
, based on WideCharToMultiByte
this time.