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.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Pisaren
  • 63
  • 5

1 Answers1

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

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • 1
    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 – taxilian Jun 25 '13 at 01:14
  • Unfortunately I need to pass this value out of my plugin. Probably I will not implement this option at all. – Pisaren Jun 25 '13 at 18:54
  • @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? – Georg Fritzsche Jun 25 '13 at 19:16
  • @GeorgFritzsche: I wanted to implement different return types(binary or base64 string) defined by input parameter, so now I will return base64 strings only. – Pisaren Jul 03 '13 at 05:35