Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert std::string* to BSTR*

79 views
Skip to first unread message

aga...@gmail.com

unread,
Jul 27, 2006, 3:34:39 AM7/27/06
to
Hi, I'm developing COM using VC++.NET ATL. I need to convert string* to
BSTR* or even to _bstr_t. I would like to know how can I achive this.
Guide me if possible with a piece of sample code.

Arun

Heinz Ozwirk

unread,
Jul 27, 2006, 5:35:28 AM7/27/06
to
<aga...@gmail.com> schrieb im Newsbeitrag
news:1153985679.6...@m73g2000cwd.googlegroups.com...

> Hi, I'm developing COM using VC++.NET ATL. I need to convert string* to
> BSTR* or even to _bstr_t. I would like to know how can I achive this.
> Guide me if possible with a piece of sample code.

First problem is that std::string is based on char while BSTR is basically a
string of wchar_t, but if you can use _bstr_t, conversion will by quite
simple. Since you mention BSTR*, I assume that it is an [out] parameter of
some method.

HRESULT SomeClass::SomeMethod(BSTR* result)
{
std::string str = "...";
_bstr_t bstr = str.c_str();
*result = bstr.Detach();
return S_OK;
}

Of cause, you could also use MultiByteToWideChar and SysAllocString,
CComBSTR or A2BSTR, but to avoid all those conversions, I usually suggest to
use Unicode instead of ANSI (or any other MBCS) inside a COM server.

HTH
Heinz

0 new messages