BSTR

A BSTR (Basic string or binary string) is a string data type that is used by COM, Automation, and Interop functions. Use the BSTR data type in all interfaces that will be accessed from script.

C++

 

typedef WCHAR OLECHAR;

typedef OLECHAR* BSTR;

typedef BSTR* LPBSTR;

 

Remarks

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

A four-byte integer that contains the number of bytes in the following data string. It appears immediately before the first character of the data string. This value does not include the terminating null character.

Data string

A string of Unicode characters. May contain multiple embedded null characters.

Terminator

Two null characters.

 
 

A BSTR is a pointer. The pointer points to the first character of the data string, not to the length prefix.

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

The following code is incorrect:

BSTR MyBstr = L"I am a happy BSTR";

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:

BSTR MyBstr = SysAllocString(L"I am a happy BSTR");

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 string as an argument to a COM function that is expecting a BSTR, the COM function will fail.

Requirements

Header

WTypes.h

BSTR的更多相关文章

  1. BSTR 的奥秘

    初学COM,总觉得BSTR很神秘,对于这种新的数据类型,总有很多疑问,那么BSTR到底是什么类型呢? 我们可以在头文件中的定义中最终找到 wchar这个类型被定义为BSTR,那么BSTR是wchar吗 ...

  2. 分配和释放 BSTR 的内存

    本文档已存档,并且将不进行维护. 分配和释放 BSTR 的内存 Visual Studio .NET 2003                  转自: https://msdn.microsoft. ...

  3. char*,wchar_t*,CString和BSTR之间的转换

    前言 本文并不尝试列举出所有的转换方法,只列举作者认为方便易用的方法. 1.char*和wchar_t*的相互转换 可以利用中间类_bstr_t(头文件comdef.h)方便的进行相互转换 const ...

  4. BSTR共享内存问题

    BSTR bstrName = OLESTR("Test String"); BSTR b1 = bstrName; BSTR b2 = bstrName; bstrName = ...

  5. BSTR、char*和CString转换

    (1) char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行.例如: char chArray[] = "This  ...

  6. 关于BSTR和SysStringLen方法的简单研究

    英文的我编不下去了,所以还是先写个中文的吧, 之前遇到了SysStringLen求Bstr长度不正确的问题,试验了几次都不行的情况下我觉得可能是这个方法的bug,所以就没管. 大概的情况是这样: in ...

  7. VC中BSTR、Char和CString类型的转换

    1.char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::format进行.例如: char chArray[] = "This is ...

  8. 【转载】CString、BSTR和LPCTSTR之间的区别

    原文:http://www.cnblogs.com/GT_Andy/archive/2011/01/18/1938605.html 一.定义 1.CString:动态的TCHAR数组.它是一个完全独立 ...

  9. 摘:分配和释放BSTR的内存

    当您创建 BSTR的并将它们使用以避免内存泄漏的它们在COM对象之间时,必须注意在将内存的.       当 BSTR 在接口中保持,必须释放其内存,当处理它.       但是,那么,当 BSTR ...

随机推荐

  1. windows下thrift的使用(python)

    1.下载thrift,下载地址:http://archive.apache.org/dist/thrift/0.9.3/ 2.在编写python的thrift代码时,需要先安装thrift modul ...

  2. 深入剖析cpp对象模型

    C++对象模型可以概括为以下2部分: 1. 语言中直接支持面向对象程序设计的部分,主要涉及如构造函数.析构函数.虚函数.继承(单继承.多继承.虚继承).多态等等. 2. 对于各种支持的底层实现机制.在 ...

  3. BZOJ 1951SDOI2010 古代猪文

    真是到很强的数学题 先利用欧拉定理A^B %p=A^(B%φ(p)+φ(p) ) %p 然后利用卢卡斯定理求出在modφ(p)的几个约数下的解 再利用中国剩余定理合并 计算答案即可 By:大奕哥 #i ...

  4. [Agc011F] Train Service Planning

    [Agc011F] Train Service Planning 题目大意: 有n+1个车站,n条轨道,第i条轨道联通i-1和i车站,通过它要花a[i]时间,这条轨道有b[i]=1或2条车道,也就是说 ...

  5. Windows Server 2008 R2的web服务器nginx和Apache的比较

    因为很喜欢nginx,所以也想尝试在Windows下使用nginx,前面安装配置都挺顺利,把域名解析尽量后,通过域名代理访问jboss,却异常的慢,起码有3秒的时间才显示页面,而这个页面是jboss的 ...

  6. pygame系列_弹力球

    这是pygame写的弹力球 运行效果: ======================================================== 代码部分: ================= ...

  7. codevs 1191 树轴染色 线段树区间定值,求和

    codevs 1191 树轴染色 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.codevs.cn/problem/1191/ Des ...

  8. redis学习之一 - linux下安装配置

    Content 0.序 1.如何安装? 2.配置参数及其意义 3.设为linux服务 0.序 本文主要是记录Redis在 Centos下的安装配置 .文中如无特别说明.表示redis-3.2.10代码 ...

  9. 小程序swiper 快速滑动闪屏

    bindchange: function(e){ if(e.detail.source == "touch") { this.setData({ current: current ...

  10. linux内核源码之基础准备篇

    http://blog.csdn.net/eastmoon502136/article/details/8711104