1 回答
![?](http://img1.sycdn.imooc.com/54584f3100019e9702200220-100-100.jpg)
TA贡献1807条经验 获得超9个赞
SysAllocString 返回 BSTR 类型,com 类型对象。
typedef struct {
#ifdef _WIN64
DWORD pad;
#endif
DWORD size;
union {
char ptr[1];
WCHAR str[1];
DWORD dwptr[1];
} u; // take it as a starting point of the string
} bstr_t;
换句话说,它是相同的 utf16 编码字符串,但前缀是其大小(Unicode 字符的长度乘以 wchar_t 的大小(2-4 字节))。出于优化的原因,它也有填充。
由于它的浮动大小,最好使用 ole 包而不是重新发明轮子。如果你想自己实现它,并且 wchar_t 的大小为 int16(2 字节),那么你必须执行以下操作:
(半伪代码,我没测试过)
type BSTR *uint16
func SysAllocString(str string) (result BSTR) {
// DWORD == int32 == rune
const padf = "\x00" // only for 64 bit system
const sizef = "\x00"
// int32 == 4 byte
// int16 == 2 byte
const wordSize = unsafe.Sizeof(int16(0))
utf16 := utf16.Encode([]rune(padf + sizef + str))
/* pad is on index 0 and 1 */
size := &utf16[2 /* 0 for 32 bit system */]
// set "size" field as unicode charachers length multypled by size of wchar_t
*(*rune)(unsafe.Pointer(size)) = rune((len(utf16)-2) * int(wordSize))
result = BSTR(&utf16[0])
return
}
// ...
bstr := SysAllocString(login.UserName)
uintptr(unsafe.Pointer(bstr))
- 1 回答
- 0 关注
- 94 浏览
添加回答
举报