tags:

views:

154

answers:

2

Hi,

What is the right way to compare two CComBSTRs? I tried to use

 bool operator ==(
     const CComBSTR& bstrSrc 
 ) const throw( );

However it always return false even two ComBSTRs are the same. It did not work correctly.

Do I have to convert CComBSTRs to ANSI string first and then use strcmp?

Thanks!

-bc

+2  A: 

You should probably use VarBstrCmp.

EDIT: this is actually what CComBSTR::operator== does, so without further context, your code may be incorrect.

MSN
VarBstrCmp doesn't work either. It returns HRESULT 0x00000002 The system cannot find the file specified. My code is like:CComBSTR m_bstrUrl;......HRESULT hr = ::VarBstrCmp( (BSTR)m_bstrUrl, pvURL->bstrVal, ::GetThreadLocale(), 0 );
bionicoder
what is the type of m_bstrUrl? why did you have to cast it to BSTR?
moogs
Read what MSDN says about the return result. The data type is an HRESULT but the meanings aren't HRESULT meanings.
Windows programmer
Thanks all! Problem solved. The issue was the source string has two weird bytes appended. It is invisible for read, but is there in comparison. Trim out extra two bytes. Everything is fine.
bionicoder
+1  A: 

BSTRs (and therefore CComBSTRs) are usually Unicode strings. You can use wcscmp() (or wcsicmp() for case-insensitive comparison).

Beware that encapsulated BSTR can be null which is a legal representation for an empty string and this should be treated as a special case, otherwise your program might run into undefined behaviour (most likely just crash).

sharptooth