Eric’s complete guide to BSTR semantics

Date:September 15, 2003 / year-entry #63
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20030915-01/?p=42483
Comments:    11
Summary:Eric Lippert has posted Eric's Complete Guide to BSTR Semantics. This is a document that is widely valued within Microsoft, since BSTR semantics are rather tricky, and I'm glad he's willing to share it with the world. In particular, Eric spends quite a bit of time discussion the very important equivalence of NULL and L""...

Eric Lippert has posted Eric's Complete Guide to BSTR Semantics. This is a document that is widely valued within Microsoft, since BSTR semantics are rather tricky, and I'm glad he's willing to share it with the world.

In particular, Eric spends quite a bit of time discussion the very important equivalence of NULL and L"" in BSTR semantics, something that is buried in paragraph seven of an MSDN document you probably didn't even bother reading.

If you ask nicely, you might be able to convince him to post his "Complete history of OLEDATE".


Comments (11)
  1. (The first link to Eric’s page is broken.)

  2. TheCodeFoundry says:

    Why waste time using BSTR when you can easily use the _bstr_t wrapper class? Is there really any advantage to using a BSTR where you have to worry about allocating and freeing memory when _bstr_t handles all of that for you?

    I’m not trolling, I would really like to understand an instance where you would not want to use _bstr_t. For me, it just seems like using the wrapper class makes your code so much cleaner.

  3. Nicholas says:

    _bstr_t is not supported on Windows CE devices. :(

    And besides it is always good to know the bases because of the law of leaky abstraction :)

  4. Raymond Chen says:

    I think you missed the point of Eric’s entry. It’s the guide to BSTR "semantics". _bstr_t helps you with BSTR memory allocation, but you still need to understand the semantics. The embedded null, the ban on internal pointers, the equivalence of NULL and a zero-length string…

  5. TheCodeFoundry says:

    Gotcha. Thanks Raymond and Nicholas for the answers.

  6. Eric Lippert says:

    CodeFoundry: bstr_t is pretty good, but in general, I avoid wrapper classes like the plague. I don’t trust them. I’ll give an example of some places where wrappers lead you astray in another entry.

  7. Eric Lippert says:

    Raymond asked for it, so I’ve posted it. My short essay on the irksome OLEAUT date format is posted on my blog.

  8. TheCodeFoundry says:

    I see Eric posted an article about smart pointers. I would like play devil’s advocate:

    If you insist on using raw COM pointers in your C++ code, if an exception is thrown, you don’t get a chance to call Release(). Now, the code that Eric posts may have had a bug in it, but if you are going to use smart pointers, you should use them everywhere. Yet his map used raw pointers. You are just asking for problems.

  9. Eric Lippert says:

    Yes, exactly my point! The only time that it is safe to use smart pointers is if you know 100% for sure that EVERYONE who EVER works on your software is going to know how to correctly use smart pointers. And that’s an unrealistic assumption in my world. I don’t know about your software projects, but there are tens of millions of lines of code in the product that I work on, in a half-dozen or more languages and who knows how many coding styles, and some of that code is ten years old or more. We have situations where C# calls C++ calls JScript calls C++ calls C…

    The whole point of a template class is that it makes it "easier" by HIDING what it is really doing behind the scenes. That hiding is great right up until you need to make some newfangled template code interoperate with a few hundred thousand lines of existing nontemplated code. And then suddenly you need to know EXACTLY what that thing does (leaky abstraction!) and the semantics are hidden in a half dozen nested templatized wrapper classes with cryptic names. Avoid, avoid, avoid.

  10. AlisdairM says:

    Effective C++ is not possible without smart pointers. There is no other general way to handle exception safety. If you don’t feel comfortable with the issue, drop back to C where SESE is a valid idiom.

    I agree that COM-wrapper pointers are very easy to get wrong and abuse, but that is no reason to throw out smart pointers in general. It simply means you must look a lot harder to find a high quality, well documented COM pointer implementation.

    [Luckily for me we don’t do much COM so I can avoid the issue in general. Outside of COM, I have found smart pointers work exceptionally well]

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index