这可能对你很有意思:
Eric's Complete Guide To BSTR Semantics
编辑:更多的细节,因为那篇文章收集:
免责声明:这关的。上面有我头和我的包含严重的错误,直到但不限于因果关系的破坏和已知宇宙的结束。
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;
}
这应该给你一个很好的想法。请注意,如果与Win32的交叉可比性很重要,则需要使用SysAllocString等来代替此代码。
此[链接显示BSTR长度前缀为4个字节](https://msdn.microsoft.com/en-us/library/windows/desktop/ms221069(v = vs.85).aspx)。我只是看内存而且我认为是真的。 – lsalamon 2015-03-10 17:06:13