BSTR
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的更多相关文章
- BSTR 的奥秘
初学COM,总觉得BSTR很神秘,对于这种新的数据类型,总有很多疑问,那么BSTR到底是什么类型呢? 我们可以在头文件中的定义中最终找到 wchar这个类型被定义为BSTR,那么BSTR是wchar吗 ...
- 分配和释放 BSTR 的内存
本文档已存档,并且将不进行维护. 分配和释放 BSTR 的内存 Visual Studio .NET 2003 转自: https://msdn.microsoft. ...
- char*,wchar_t*,CString和BSTR之间的转换
前言 本文并不尝试列举出所有的转换方法,只列举作者认为方便易用的方法. 1.char*和wchar_t*的相互转换 可以利用中间类_bstr_t(头文件comdef.h)方便的进行相互转换 const ...
- BSTR共享内存问题
BSTR bstrName = OLESTR("Test String"); BSTR b1 = bstrName; BSTR b2 = bstrName; bstrName = ...
- BSTR、char*和CString转换
(1) char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行.例如: char chArray[] = "This ...
- 关于BSTR和SysStringLen方法的简单研究
英文的我编不下去了,所以还是先写个中文的吧, 之前遇到了SysStringLen求Bstr长度不正确的问题,试验了几次都不行的情况下我觉得可能是这个方法的bug,所以就没管. 大概的情况是这样: in ...
- VC中BSTR、Char和CString类型的转换
1.char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::format进行.例如: char chArray[] = "This is ...
- 【转载】CString、BSTR和LPCTSTR之间的区别
原文:http://www.cnblogs.com/GT_Andy/archive/2011/01/18/1938605.html 一.定义 1.CString:动态的TCHAR数组.它是一个完全独立 ...
- 摘:分配和释放BSTR的内存
当您创建 BSTR的并将它们使用以避免内存泄漏的它们在COM对象之间时,必须注意在将内存的. 当 BSTR 在接口中保持,必须释放其内存,当处理它. 但是,那么,当 BSTR ...
随机推荐
- 决策树算法(Bagging与随机森林)
Bagging算法: 将训练数据集进行N次Bootstrap采样得到N个训练数据子集,对每个子集使用相同的算法分别建立决策树,最终的分类(或回归)结果是N个决策树的结果的多数投票(或平均). 其中,B ...
- C# 简单读写ini文件帮助类 INIHelp
软件里需要读取一些初始化信息, 决定用ini来做,简单方便. 于是查了一写代码,自己写了一个帮助类. INI文件格式是某些平台或软件上的配置文件的非正式标准, 以节(section)和键(key)构成 ...
- 写在OI退役后和高中毕业前的一些话
更新日志: 2017.02.13 开坑 2017.02.13 更新[零][壹] 2017.02.14 更新[贰] 2017.02.26 更新[叁][肆] 2017.03.04 锅多如狗,停更一周 20 ...
- 【Floyd矩阵乘法】BZOJ1706- [usaco2007 Nov]relays 奶牛接力跑
[题目大意] 给出一张无向图,求出恰巧经过n条边的最短路. [思路] 首先题目中只有100条边,却给出了10000个点(实际上最多只能有200个),离散化一下. 后面就是Floyd的新姿势,以前看过的 ...
- spring boot2集成ES详解
一:运行环境 JDK:1.8 ES:5.6.4 二:学习内容 如何构建spring-data-elasticsearch环境? 如何实现常用的增删改查? 如何实现对象嵌套也就是1对多这种关系? 三:J ...
- 升级Tornado到4后weibo oauth登录不了
把 Tornado 升级到4后,发现正常运行的微博登录不可以了. 原因是4已经移除 RequestHandler.async_callback and WebSocketHandler.async_c ...
- Elasticsearch基础分布式架构
写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...
- UVALive 5971
Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it ...
- php的哈希函数
哈希函数: echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n"; 验证函数: boolean ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...