【发布时间】:2014-03-14 12:59:35
【问题描述】:
如何连接CComBSTR和字符串?
我试过这种方法:
CComBSTR a = "DEF";
CComBSTR strRequete = "ABC'" + a + "GHI"; //marked line
但我在 a 的 marked line 告诉中遇到错误:
错误:表达式必须具有整数或无范围枚举类型。
非常感谢!
【问题讨论】:
如何连接CComBSTR和字符串?
我试过这种方法:
CComBSTR a = "DEF";
CComBSTR strRequete = "ABC'" + a + "GHI"; //marked line
但我在 a 的 marked line 告诉中遇到错误:
错误:表达式必须具有整数或无范围枚举类型。
非常感谢!
【问题讨论】:
要连接,你有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
【讨论】:
Append 方法,它们接受不同的类型。一个需要CComBSTR 的版本。因此,如果要附加 + 符号,请使用后者。在这种情况下没有最佳实践。 Append 对我来说更冗长,可以看起来更好。
+=。
(SQLWCHAR*)strSentence 吗?
微软说:
CComBSTR 类是 BSTRs 的包装器,它们是长度前缀字符串。
要么你必须使用CComBSTR::Append 或
bstr = CStringW(L"String2") + bstr
【讨论】: