3

How to test if a CComBSTR is an empty string? (with no 'text' value, can be "" or can be null)

my ideas:

  1. test if CComBSTR::ByteLength() returns 0
  2. test if CComBSTR::GetStreamSize() returns 0
  3. test if CComBSTR::m_str is NULL
  4. test if CComBSTR::Length() returns 0

which one is correct approach? if none of them is, then what is correct approach?

thanks.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Hayri Uğur Koltuk
  • 2,970
  • 4
  • 31
  • 60

2 Answers2

2

4) Test length 0 it's fast as it's stored

Tom
  • 43,583
  • 4
  • 41
  • 61
0

3) test if CComBSTR::m_str is NULL

If you check the source code of CComBSTR there's several operators you can use to do this test:

bool CComBSTR::operator!() const throw()
bool CComBSTR::operator!=(int nNull) const throw()
bool CComBSTR::operator==(int nNull) const throw()
operator CComBSTR::BSTR() const throw()

For example:

CComBSTR value;
if (!value) { /* NULL */ } else { /* not NULL */ }
if (value != NULL) { /* not NULL */ } else { /* NULL */ }
if (value == NULL) { /* NULL */ } else { /* not NULL */ }
if ((BSTR) value) { /* not NULL */ } else { /* NULL */ }
Stephen Quan
  • 21,481
  • 4
  • 88
  • 75