问题
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
回答1:
You should probably use VarBstrCmp.
EDIT: this is actually what CComBSTR::operator== does, so without further context, your code may be incorrect.
回答2:
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).
回答3:
BSTRsAreEqual(BSTR bstr1, BSTR bstr2, VARIANT_BOOL* boolptrEqual)
{
CString s1, s2;
s1 = bstr1;
s2 = bstr2;
if (s1 == s2) {
*boolptrEqual = true;
} else {
*boolptrEqual = false;
}
}
来源:https://stackoverflow.com/questions/1430061/how-to-compare-two-bstrs-or-ccombstrs