Domanda

I have a piece of code that gets random binary string from CAPICOM.Utilities.

m_pUtilities.CreateInstance(__uuidof(Utilities));
_bstr_t bstrResult;
m_pUtilities->raw_GetRandom(64, CAPICOM_ENCODE_BINARY, bstrResult.GetAddress());

I made a method that returns

return std::wstring(bstrResult);

My method crashes plugin appr. 1 of 6 calls. The exception description is Unhandled exception at 0x7572969b in chrome.exe: Microsoft C++ exception: utf8::invalid_utf16 at memory location. I tried to change return type of my method from FB::variant to std::wstring, but this didn't help.

What I did wrong? How should I return the binary string? Converting the binary string to base64 or other changes in return string is not suitable for me.

È stato utile?

Soluzione

If you want to pass this as a string to JavaScript you really need to use CAPICOM_ENCODE_BASE64 - otherwise the values in this buffer could be anything, including values outside the character space. Some code obviously checks for this, causing your exception.

If you only need to use this internally in your plugin, don't use strings but e.g. a std::vector<WCHAR> or just use the BSTR.

Side note: it won't matter in this case, but BSTRs are length prefixed and both BSTRs and std::wstrings can contain embedded 0s - so to be correct you really would have to return std::wstring(bstr.GetBSTR(), bstr.length()).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top