问题描述
当维护 COM
接口时,应将空 BSTR
视为 NULL
?
换句话说,这两个函数调用是否产生相同的结果?
When maintaining a COM
interface should an empty BSTR
be treated the same way as NULL
?In other words should these two function calls produce the same result?
// Empty BSTR
CComBSTR empty(L""); // Or SysAllocString(L"")
someObj->Foo(empty);
// NULL BSTR
someObj->Foo(NULL);
推荐答案
是 - 空BSTR与空一。我记得当我们从VS6切换到2003时,我们遇到了各种各样的错误 - CComBSTR类改变了默认的构造函数,它使用NULL而不是空字符串来分配它。这种情况发生在你例如把一个BSTR当作一个常规的C风格字符串,并把它传递给某些函数,如 strlen
,或者尝试初始化一个 std:
Yes - a NULL BSTR is the same as an empty one. I remember we had all sorts of bugs that were uncovered when we switched from VS6 to 2003 - the CComBSTR class had a change to the default constructor that allocated it using NULL rather than an empty string. This happens when you for example treat a BSTR as a regular C style string and pass it to some function like strlen
, or try to initialise a std::string
with it.
Eric Lippert在:
Eric Lippert discusses BSTR's in great detail in Eric's Complete Guide To BSTR Semantics:
1)BSTR必须有相同的
NULL和的语义。
1) A BSTR must have identical semantics for NULL and for "". A PWSZ frequently has different semantics for those.
2)BSTR必须被分配并释放
与SysAlloc *系列的
函数。 PWSZ可以是
栈中的
自动存储缓冲区,或者分配有malloc,new,
LocalAlloc或任何其他内存
分配器。
2) A BSTR must be allocated and freed with the SysAlloc* family of functions. A PWSZ can be an automatic-storage buffer from the stack or allocated with malloc, new, LocalAlloc or any other memory allocator.
3)BSTR是固定长度的。 PWSZ
可以是任何长度,只受
其
缓冲区中的有效内存量的限制。
3) A BSTR is of fixed length. A PWSZ may be of any length, limited only by the amount of valid memory in its buffer.
4)A BSTR总是指向缓冲区中的第一个
有效字符。
4) A BSTR always points to the first valid character in the buffer. A PWSZ may be a pointer to the middle or end of a string buffer.
5)当分配一个n字节的BSTR时,
可以是一个指向字符串缓冲区的中间或结束
的指针。 b $ b有n / 2个宽字符的空间。
当为PWSZ
分配n个字节时,您可以存储n / 2 - 1个字符 -
,您必须为null留出空间。
5) When allocating an n-byte BSTR you have room for n/2 wide characters. When you allocate n bytes for a PWSZ you can store n / 2 - 1 characters -- you have to leave room for the null.
6)BSTR可以包含任何Unicode数据
,包括零字符。 PWSZ
除非作为字符串结尾标记,否则不包含零字符
。
BSTR和PWSZ在最后一个有效的
字符之后总是有一个
零字符,但在BSTR中,有效的
字符可能是零字符。
6) A BSTR may contain any Unicode data including the zero character. A PWSZ never contains the zero character except as an end-of-string marker. Both a BSTR and a PWSZ always have a zero character after their last valid character, but in a BSTR a valid character may be a zero character.
7)BSTR实际上可能包含奇数
字节数 - 它可用于
移动二进制数据。 PWSZ是
,几乎总是偶数字节
,并且仅用于存储Unicode
字符串。
7) A BSTR may actually contain an odd number of bytes -- it may be used for moving binary data around. A PWSZ is almost always an even number of bytes and used only for storing Unicode strings.
这篇关于空白BSTR和空BSTR之间应该有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!