This may be of interest to you:
Eric's Complete Guide to BSTR Semantics
EDIT: Some additional information derived from this article:
DISCLAIMER: This does not correspond to my head, and I have serious errors, up to the destruction of causality and the end of the known universe.
struct BSTR_data {
short count;
wchar_t[] data;
};
typedef wchar BSTR;
BSTR * AllocateBSTR(wchar * str) {
if(str == 0) return 0;
short len = wstrlen(str);
BSTR_data * ret = new char[sizeof(short) + (sizeof(wchar_t) + 1) * len];
ret->count = len;
memcpy(ret->data, str, sizeof(wchar_t) * 2 * len);
ret->data[len] = 0;
return (BSTR *)(ret + sizeof(short));
}
void DeallocateBSTR(BSTR * str) {
if(str == 0) return;
BSTR_data * bstr = (BSTR_data*)(str - sizeof(short));
delete bstr;
}
This should give you a good idea of what is happening. Note that if cross-compatibility with Win32 is important, you need to use SysAllocString, etc. Instead of this code.
source
share