【问题标题】:How to concatenate CComBSTR and strings?如何连接 CComBSTR 和字符串?
【发布时间】:2014-03-14 12:59:35
【问题描述】:

如何连接CComBSTR和字符串?

我试过这种方法:

CComBSTR a = "DEF";
CComBSTR strRequete = "ABC'" + a + "GHI"; //marked line

但我在 amarked line 告诉中遇到错误:

错误:表达式必须具有整数或无范围枚举类型。

非常感谢!

【问题讨论】:

    标签: c++ string


    【解决方案1】:

    要连接,你有8 methods:

    HRESULT Append(LPCOLESTR lpsz, int nLen);             
    HRESULT Append(LPCOLESTR lpsz);                       
    HRESULT Append(LPCSTR);                               
    HRESULT Append(char ch);                              
    HRESULT Append(wchar_t ch);                           
    
    HRESULT Append(const CComBSTR& bstrSrc);          
    CComBSTR& operator+=(const CComBSTR& bstrSrc);
    
    HRESULT AppendBSTR(BSTR p);  
    

    使用 += 运算符,您可以像这样追加:

    CComBSTR strSentence = OLESTR("Now is the time ");
    
    // strSentence contains "Now is the time "
    CComBSTR str11 (OLESTR("for all good men ");
    // calls Append(const CComBSTR& bstrSrc);
    strSentence.Append(str11);
    // strSentence contains "Now is the time for all good men "
    // calls Append(LPCOLESTR lpsz);
    strSentence.Append((OLESTR("to come "));
    // strSentence contains "Now is the time for all good men to come "
    // calls Append(LPCSTR lpsz);
    strSentence.Append("to the aid ");
    // strSentence contains
    // "Now is the time for all good men to come to the aid "
    
    CComBSTR str12 (OLESTR("of their country"));
    strSentence += str12; // calls operator+=()
    // "Now is the time for all good men to come to
    // the aid of their country"
    

    更多信息请访问此链接:http://www.369o.com/data/books/atl/0321159624/ch02lev1sec3.html

    【讨论】:

    • 哪个是最好的做法?
    • @Luciekulza:你有 6 个重载的 Append 方法,它们接受不同的类型。一个需要CComBSTR 的版本。因此,如果要附加 + 符号,请使用后者。在这种情况下没有最佳实践。 Append 对我来说更冗长,可以看起来更好。
    • 您能像 FredrickGauss 那样提供一个简短的例子吗?
    • @Luciekulza:示例在第二个块中。看看最后一个+=
    • 感谢您的回复,请再问一个问题:要将 CComBSTR 转换为 SQLWCHAR*,我需要简单的 (SQLWCHAR*)strSentence 吗?
    【解决方案2】:

    微软说:

    CComBSTR 类是 BSTRs 的包装器,它们是长度前缀字符串。

    要么你必须使用CComBSTR::Append

    bstr = CStringW(L"String2") + bstr

    Refer to.

    【讨论】:

      猜你喜欢
      • 2016-03-10
      • 2023-03-08
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2011-02-12
      相关资源
      最近更新 更多