Question

I'm working with a Microsoft Kinect SDK where functions return BSTR. I need to get a QString or std::string.

Here's what I've tried:

BSTR bstr = s->NuiUniqueId();

// QString qs((QChar*)bstr, SysStringLen(bstr));

std::wstring ws(bstr);

ui->lblDetails->setText(QString::fromStdWString(ws));

With this solution the program crashes. With the line that is commented out I get "unresolved external symbol SysStringLen".

Is SysStringLen the way to go, but I need to add some additional libraries (wouldn't the API include it already) or is there another solution?


Additional question: why does Microsoft do it? I mean:

#if !defined(_NATIVE_WCHAR_T_DEFINED)
typedef unsigned short WCHAR;
#else
typedef wchar_t WCHAR;
#endif
typedef WCHAR OLECHAR;
typedef OLECHAR* BSTR;
typedef BSTR* LPBSTR;

What's the reason behind stuff like this? And even if they find it beneficial to use it internally, couldn't they just use normal char array or std::(w)string in the API to make other's life easier?

Was it helpful?

Solution

You can convert the BSTR object to char *, then convert it to QString. Here:

QString *getQStringFromBstr(BSTR bstrVal){
  char *p= const_cast<char *>(_com_util::ConvertBSTRToString(bstrVal));
  return new QString(p);
}

COM was designed to be Language-agnostic binary equalizer. Which means I could use a VB function in C++ and C++ function in, say, C#(with COM interop). This is the reason most of the strings and few functions were changed to language neutral strings IIRC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top