0

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.

1 Answer 1

2

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()).

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah; you can't return binary strings from an npapi plugin to the browser, it's just not supported. The browsers expect a utf8-encoded string
Unfortunately I need to pass this value out of my plugin. Probably I will not implement this option at all.
@Pisaren: Technically you could fill a (JS) array with the plain values and return that, but that would be very slow. How about passing it as base64 to JS and decode it to a number array there?
@GeorgFritzsche: I wanted to implement different return types(binary or base64 string) defined by input parameter, so now I will return base64 strings only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.