1

我是_bstr_t's 的新手,但仍在尝试掌握它。我试图检查特定字符串x是否包含在 bstring 中的任何位置。我通常会喜欢的东西;

String x = "hello";
String example = "You! hello there";
...
if (example.find(x) != string::npos) {
...

只是为了记录,预期的平台是windows。

4

2 回答 2

4

There is no need to use _bstr_t. Use the BSTR type.

Next, read Eric's Complete Guide to BSTR Semantics.

Lastly, you can use the BSTR in native code the way you would a normal character array in most cases.

BSTR bstr = SysAllocString(L"FooBarBazQux");
if (wcsstr(bstr, L"Bar") != NULL) {
  // Found it! Do something.
} else {
  // Not there.
}
SysFreeString(bstr);

MSDN for wcsstr.

于 2011-10-06T16:01:33.823 回答
1

Your example appears to be trying to use string::find from STL. But you specify your variables of type "String" (capitalized). If you instead did:

using namespace std;
string x = "hello";
string example = "You! hello there";
...

your example would compile. Do you actually have a BSTR or _bstr_t that you need to work with that you haven't shown? Even if so, it's pretty easy to make an std::string from a _bstr_t, and after that you can use STL as you normally would.

于 2011-10-08T21:10:49.507 回答