1 回答
TA貢獻1807條經(jīng)驗 獲得超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 編碼字符串,但前綴是其大?。║nicode 字符的長度乘以 wchar_t 的大?。?-4 字節(jié)))。出于優(yōu)化的原因,它也有填充。
由于它的浮動大小,最好使用 ole 包而不是重新發(fā)明輪子。如果你想自己實現(xiàn)它,并且 wchar_t 的大小為 int16(2 字節(jié)),那么你必須執(zhí)行以下操作:
(半偽代碼,我沒測試過)
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 關注
- 145 瀏覽
添加回答
舉報
