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);
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 astd::stringwith it.Eric Lippert discusses BSTR’s in great detail in Eric’s Complete Guide To BSTR Semantics: