VC学习四:关于 BSTR, CComBSTR and _bstr_t
2012-01-10 17:15
295 查看
BSTR是COM中默认的字符串数据格式,和char* 及std::string等不同,BSTR is a null-terminated,
length-prefixed, unicode string.
下面是一个非常好的对BSTR的解释: A bstr is a pointer to a string of wide char. The string is not terminated. Instead, the length of the string is stored as an unsigned long just before the first character of the string. (这一点和普通的 *char很不同,因为*char 指针指向的是该串的第一个字符,而bstr的指针是指向该字符串的长度)
You might think that you could create a bstr the c++ way, by creating a structure and then returning a pointer to the second element, cast to bstr, but you should never do this! the memory pointed to by a bstr is actually owned by Windows. Becaust a bstr's
memory is owned by Windows, you can safely pass a bstr between processes. Instead, you can use the function
SysAllocString() to create a bstr, and the function SysFreeString() to destroy it properly. In fact, you should only use platform SDK functions (or helper classes that use them internally) to manipulate bstrs.
缺点: most of the string manipulation functions normaly taken for granted, such as finding a substring, comparing two strings, and so on, are
missing. Most significantly, there seems to be no function to copy a bstr.
CComBSTR: 是ATL中的一个类, this class is wrapper for BSTR,
_bstr_t: 是BSTR的封装类,实际上是一个smart pointer, it encapsulates the BSTR date type. It manages resource allocation and deallocation through function calls to SysAllocString / SysFreeString and other BSTR APIs. _bstr_t 其实相当于一个带有引用计数的智能指针。
_bstr_t: is "native COM support" class. And CComBSTR is the ATL class. the two classes are very similar. Both will take care of allocating and deallocating the wrapped bstr in the class constructor and destructor. They also can be used to take charge
of an existing bstr with the Attach() method, or can be made to abandon the bstr with Detach() method. The have helpful operators and methods to compare two string, copy a string, and so on. In many cases, _bstr_t and CComBSTR instances can be passed as bstr
substitues. ( 但要注意:有时候不恰当的使用这两个类来代替bstr有可能会造成内存泄露)
CComBSTR和 _bstr_t 都是BSTR的wrapper,但实现机制不同, _bstr_t更通用些,不过如果使用ATL的话,可能 CComBSTR更方便些。
length-prefixed, unicode string.
下面是一个非常好的对BSTR的解释: A bstr is a pointer to a string of wide char. The string is not terminated. Instead, the length of the string is stored as an unsigned long just before the first character of the string. (这一点和普通的 *char很不同,因为*char 指针指向的是该串的第一个字符,而bstr的指针是指向该字符串的长度)
You might think that you could create a bstr the c++ way, by creating a structure and then returning a pointer to the second element, cast to bstr, but you should never do this! the memory pointed to by a bstr is actually owned by Windows. Becaust a bstr's
memory is owned by Windows, you can safely pass a bstr between processes. Instead, you can use the function
SysAllocString() to create a bstr, and the function SysFreeString() to destroy it properly. In fact, you should only use platform SDK functions (or helper classes that use them internally) to manipulate bstrs.
缺点: most of the string manipulation functions normaly taken for granted, such as finding a substring, comparing two strings, and so on, are
missing. Most significantly, there seems to be no function to copy a bstr.
CComBSTR: 是ATL中的一个类, this class is wrapper for BSTR,
_bstr_t: 是BSTR的封装类,实际上是一个smart pointer, it encapsulates the BSTR date type. It manages resource allocation and deallocation through function calls to SysAllocString / SysFreeString and other BSTR APIs. _bstr_t 其实相当于一个带有引用计数的智能指针。
_bstr_t: is "native COM support" class. And CComBSTR is the ATL class. the two classes are very similar. Both will take care of allocating and deallocating the wrapped bstr in the class constructor and destructor. They also can be used to take charge
of an existing bstr with the Attach() method, or can be made to abandon the bstr with Detach() method. The have helpful operators and methods to compare two string, copy a string, and so on. In many cases, _bstr_t and CComBSTR instances can be passed as bstr
substitues. ( 但要注意:有时候不恰当的使用这两个类来代替bstr有可能会造成内存泄露)
CComBSTR和 _bstr_t 都是BSTR的wrapper,但实现机制不同, _bstr_t更通用些,不过如果使用ATL的话,可能 CComBSTR更方便些。
相关文章推荐
- 关于 BSTR, CComBSTR and _bstr_t
- vc学习之关于缩放到托盘区
- iphone/ipad关于size, frame and bounds总结和UIScroll view学习笔记
- VC中BSTR、Char*、CString和CComBSTR类型的转换
- 关于VC++中FindWindow的学习!
- 关于VC++中FindWindow的学习!
- 关于vc调试出现One or more breakpoints cannot be set and have been disabled
- 关于vc调试出现One or more breakpoints cannot be set and have been disabled
- 孙鑫VC学习笔记:第十三讲 关于释放内存
- VC学习摘要1——关于windows程序的一般原理
- 关于新手学习VC/MFC的前途的讨论
- 关于VC++_MFC的学习心得
- 实习期间关于VC/C++的一点学习笔记
- VC中BSTR、Char*、CString和CComBSTR类型的转换
- 摘要:关于VC中的数据类型转换BSTR、char*和CString的深入研究。
- VC++中遇到的各种数据类型BSTR、LPSTR、LPWSTR、CString、VARIANT、COleVariant 、_variant_t、CComBSTR
- 关于vc调试出现One or more breakpoints cannot be set and have been disabled解决方法
- Tapestry学习七:关于组件(二)TextField, PasswordField and PageLink
- 关于VC2005的学习感言
- iphone/ipad关于size, frame and bounds总结和UIScroll view学习笔记