BSTR 简介

A BSTR (Basic string or binary string) is a string data type that is used by COM, Automation, and Interop functions. A BSTR is a composite data type that consists of a length prefix, a data string, and a terminator. The following table describes these components.  

Item

Description

Length prefix

Ÿ   Consists of a four-byte integer.

Ÿ   Occurs immediately before the first character of the data string.

Ÿ   Contains the number of bytes in the following data string.

Ÿ   Does not include the terminator.

Data string

Ÿ   On Microsoft Windows, consists of a string of Unicode characters (wide or double-byte characters).

Ÿ   On Apple Power Mac, consists of a single-byte string.

Ÿ   May contain multiple embedded null characters.

Terminator

Ÿ   Consists of two null characters (0x00).

A BSTR is a pointer. It is defined in OLEAuto.h as follows:

The pointer points to the first character of the data string, not to the length prefix.

BSTRs are also referred to as a string of OLECHARs. The OLECHAR data type is defined in WTypes.h as follows: 

Use the BSTR data type in all interfaces that will be accessed from Visual Basic or from Java applets.

BSTRs are allocated using COM memory allocation functions, so they can be returned from methods without concern for memory allocation.

Remarks

The following code is incorrect:

This code builds (compiles and links) correctly, but it will not function properly because the string does not have a length prefix. If you use a debugger to examine the memory location of this variable, you will not see a four-byte length prefix preceding the data string.

Instead, use the following code:

A debugger that examines the memory location of this variable will now reveal a length prefix containing the value 34. This is the expected value for a 17-byte single-character string that is converted to a wide-character string through the inclusion of the "L" string modifier. The debugger will also show a two-byte terminating null character (0x0000) that appears after the data string.

If you pass a simple Unicode (wchar) string as an argument to a COM function that is expecting a BSTR, the COM function will fail.

 

From: MSDN

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值