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更方便些。