How to compare two BSTRs or CComBSTRs?

十年热恋 提交于 2019-12-02 07:40:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!