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

How to copy a BSTR ?

2,117 views
Skip to first unread message

Gabriel Kniznik

unread,
Jul 31, 2000, 3:00:00 AM7/31/00
to

SysFreeString(country);
country = SysAllocString(newVal);

Gabriel Kniznik


"ken" <k...@1sthost.com> wrote in message news:#oiVtjx#$GA.60@cppssbbsa04...
> Hi,
>
> I need to copy a BSTR to a BSTR, how to do that?
>
> BSTR country=SysAllocString(L"US");
>
> newVal="UK"; from function input
>
> country=newVal??????
> use memcpy, how to deal with the string length?
>
> thanks
>
>

Steve Long

unread,
Jul 31, 2000, 3:00:00 AM7/31/00
to

_bstr_t (which encapsulates a BSTR) has its own overloaded operators. You
can just use those also.

Steve

ken

unread,
Aug 1, 2000, 3:00:00 AM8/1/00
to

Howard M. Swope III

unread,
Aug 1, 2000, 3:00:00 AM8/1/00
to
You could use the ATL CComBSTR class.

myfunc(BSTR val)
{
CComBSTR country;

country = val;
}

CComBSTR handles much of the memory allocation / destruction for you. Also
there is the compiler supported BSTR class _bstr_t that has similar
functionality.

--
Howard M. Swope III [hsw...@spitzinc.com]
Software Engineer
Spitz, Inc. [http://www.spitzinc.com]

"ken" <k...@1sthost.com> wrote in message news:#oiVtjx#$GA.60@cppssbbsa04...

Robert Dettmann

unread,
Aug 2, 2000, 3:00:00 AM8/2/00
to

Hi,
sorry, the previous entries are not so good, because you can get memory
problems.
Here are the only three methods to solve your problem:

BSTR bstrSource = L"Source";
BSTR bstrTarget;

(Case 1 - ATL-Inner module call)

CComBSTR bstrSource( bstrSource );

(Case 2 - ATL-Interface output retval parameter)

bstrSource.CopyTo( &bstrTarget );

(Case 3 - simplest, but unstable state in ATL-modules)

bstrTarget = _bstr_t( bstrSource ).copy();

Test it:
::MessageBox( NULL, ( char* )_bstr_t( bstrTarget ), "Result", MB_OK );


0 new messages