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

时间: 2024-08-30 12:43:38

BSTR的相关文章

BSTR使用误区以及隐藏的内存破坏和内存泄漏

作者:magictong 简介 BSTR的数据结构是什么样子并不是本文讨论的问题,但是却是本文的基础.在解决COM的跨平台编程的问题时,需要定义一种通用的字符串类型,它就这样被发明了,而且它的结构很容易匹配到不同的编程环境中,对于C++程序员来说,要记住的最基本的一点就是分配BSTR结构时,并不是简单的调用new.malloc就可以完成的,而且大部分的字符串相关的API和C库函数也是不能用于处理BSTR的,其实这也是使用BSTR的误区之一,在C++里面,BSTR被简单的define为wchar_

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

原文:http://www.cnblogs.com/GT_Andy/archive/2011/01/18/1938605.html 一.定义 1.CString:动态的TCHAR数组.它是一个完全独立的类,封装了+等操作符和字符串操作方法. 2.BSTR:专有格式的字符串(需要使用系统函数来操纵).定义为:typedef OLECHAR FAR* BSTR 3.LPCTSTR:常量的TCHAR指针.定义为:typedef const char* LPCTSTR 二.要点 1.char*:指向AN

关于BSTR数据类型

关于BSTR数据类型 - 极品垃圾 - C++博客 http://www.cppblog.com/bestcln/articles/82712.html 1.COM字符串类型字符串的长度可能互不相同,因此跨COM边界传输特定的字符串时,需要确定它的长度,而且,字符串有时需要 分配内存. 2.Unicode和ANSI数据类型 3.OLECHAR,LPOLESTR,LPCOLESTRCOM的基本字符数据类型是OLECHAR,与平台无关的字符表示法.在创建该字符集时,OLECHAR的基本数据类型随操

谈ATL(六)--BSTR和CComBSTR类

在我写的谈ATL(四)--VARIANT和CComVariant中详细分析了VARAINT类型的本质,并详细说明了CComVariant为什么可以完全替代VARAINT的理由,下面我打算把BSTR和CComBSTR也详细的说明一下,不过与VARAINT和CComVariant的关系不同的是,CComVariant是VARAINT的子类,在传递参数时,利用的是子类对象is-a父类的概念.BSTR和CComBSTR是不是也是这种关系呢?不是的!我们先来看看BSTR的定义: typedef OLECH

谈ATL(二)--BSTR与CComBSTR

关于BSTR类 BSTR类型的实质是指向一个带长度前缀的OLECHAR字符数组的指针. BSTR是指针数据类型.它指向数组的第一个字符,长度是以整数存储的数据中紧接第一个字符前面的位置. BSTR中的字符数组以NUL字符结束. 前缀长度以字节单位,描述的是字符串的长度,该长度不包括终止字符NUL. 字符数组内部可以包括有效的NUL字符. BSTR必须使用SysAllocString和SysFreeString函数族进行分配和释放. NULL的BSTR指针表示空字符串.简单讲就是BSTR *p =

_bstr_t与BSTR

问题: BSTR   a   =   _bstr_t("a"); BSTR   b   =   _bstr_t("b"); CString   c   ; c   =   a; MessageBox(c); c   =   b; MessageBox(c); 为什么消息框中显示的都是   b? 如果这样: _bstr_t   bstr1("a"); BSTR   a   =   bstr1; _bstr_t   bstr2("b&quo

自定义TCHAR2BSTRh函数和BSTR内存的释放

1 inline HRESULT TCHAR2BSTR(TCHAR * szBuf, BSTR * bstrNew) 2 { 3 try 4 { 5 WCHAR * wszBuf; 6 #ifndef UNICODE 7 wszBuf = new WCHAR[512]; 8 MultiByteToWideChar(CP_ACP, 0, szBuf, -1, wszBuf, 512); 9 #else 10 wszBuf = szBuf; 11 #endif 12 13 bstrNew = ::S

ATL接口返回类型&&ATL接口返回字符串BSTR*

感觉在ATL中做COM组件,添加方法的时候,其返回值只能是HRESULT,我想返回其他数据类型,可以吗? 非也非也 HRESULT指示返回的状态,即正确与否, 返回值是这样的 HRESULT MyMethod([in]int p1,[out,retval]int *pRetval); 在VB和脚本里就可以n=obj.MyMethod(123) 这是COM规范规定的,其他同上 COM规范并没有规定接口成员函数一定要返回HRESULT,可以返回其它类型的值,使用其它类型值时,组件没有远程能力,组件不

读SAFEARRAY,内容是BSTR一维数组

参考:https://msdn.microsoft.com/zh-cn/library/ms891243 SAFEARRAY* psa = NULL;//=new SAFEARRAY(); ipFissSchemeManager->get_Schemes(QStringConvertion::QStr2_bstr_t(strSchemeTable), &psa); long lBound = 0, uBound = 0; SafeArrayGetLBound(psa, 1, &lBo

CString,int,String,char,BSTR之间的转换

String 转 CString CString.format("%s", string.c_str()); char 转 CString CString.format("%s", char*); char 转 string string s(char *); string 转 char * char *p = string.c_str(); CString 转 string string s(CString.GetBuffer()); 1. String ->