Click to See Complete Forum and Search --> : How to cast string to BSTR?


Jon Davis
09-25-2000, 07:00 AM
I guess I can typecast ostringstream to a string. But now how do I turn
that to a BSTR? The following clueless code fails to compile.

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <comdef.h>
using namespace std;

int main(int argc, char* argv[])
{
std::ostringstream os;
os << "Hello World!" << endl;
string strv = os.str();
_bstr_t asdf(strv);
cout << (BSTR)asdf;
return 0;
}


Jon
"Jon Davis" <jond_123@REMOVE.ME.yahoo.com> wrote in message
news:eWl11ytJAHA.258@cppssbbsa04...
> I'm having a **** of a time trying to figure this one out. How do you
cast
> an ostringstream type object to a BSTR variable????? Any help would be
> appreciated...
>
> Jon
>
>

Wayne Mack
09-25-2000, 07:33 AM
You can't cast to a BSTR, but you can construct a _bstr_t wrapper from a const
char *. Then you can extract the BSTR with const wchar_t *.

_bstr_t asdf(strv.c_str());
cout << (const wchar_t *)asdf;

Note that std::cout does not take a BSTR and that a BSTR is not the same
structure as a wchar_t *, even though the compiler treats them the same.

Jon Davis
09-25-2000, 01:33 PM
Thanks. This leads me to another question, though. In my test run I take
the variable and pass it to cout. Why do I get a number, 001349B4, rather
than the value of the string? Here's my code:

std::ostringstream os;
os << "Hello World!" << endl;
string strv = os.str();
_bstr_t asdff(strv.c_str());
cout << (const wchar_t *)asdff;

Jon


"Wayne Mack" <wmack@pec.com_nospam> wrote in message
news:39cf3825$1@news.devx.com...
>
> You can't cast to a BSTR, but you can construct a _bstr_t wrapper from a
const
> char *. Then you can extract the BSTR with const wchar_t *.
>
> _bstr_t asdf(strv.c_str());
> cout << (const wchar_t *)asdf;
>
> Note that std::cout does not take a BSTR and that a BSTR is not the same
> structure as a wchar_t *, even though the compiler treats them the same.
>
>

chris
09-25-2000, 03:04 PM
"Jon Davis" <jond_123@REMOVE.ME.yahoo.com> wrote:
>Thanks. This leads me to another question, though. In my test run I take
>the variable and pass it to cout. Why do I get a number, 001349B4, rather
>than the value of the string? Here's my code:
>
> std::ostringstream os;
> os << "Hello World!" << endl;
> string strv = os.str();
> _bstr_t asdff(strv.c_str());
> cout << (const wchar_t *)asdff;
>
>Jon
>
>
>"Wayne Mack" <wmack@pec.com_nospam> wrote in message
>news:39cf3825$1@news.devx.com...
>>
>> You can't cast to a BSTR, but you can construct a _bstr_t wrapper from
a
>const
>> char *. Then you can extract the BSTR with const wchar_t *.
>>
>> _bstr_t asdf(strv.c_str());
>> cout << (const wchar_t *)asdf;
>>
>> Note that std::cout does not take a BSTR and that a BSTR is not the same
>> structure as a wchar_t *, even though the compiler treats them the same.
>>
>>
>
>

b/c you are not using the unicode version of cout (wcout) you are inserting
the value of the ptr into the output stream, not the value being pointed
to.

try this:

int main()
{
std::ostringstream os;
os << "Hello World!" << endl;
string strv = os.str();
_bstr_t asdff(strv.c_str());
wcout << static_cast<const wchar_t*>(asdff);
return 0;
}

Jon Davis
09-28-2000, 03:09 AM
Thanks!

Jon


"chris" <chofstaedter@padcomusa.com> wrote in message
news:39cfa1ae$1@news.devx.com...
>
> "Jon Davis" <jond_123@REMOVE.ME.yahoo.com> wrote:
> >Thanks. This leads me to another question, though. In my test run I
take
> >the variable and pass it to cout. Why do I get a number, 001349B4,
rather
> >than the value of the string? Here's my code:
> >
> > std::ostringstream os;
> > os << "Hello World!" << endl;
> > string strv = os.str();
> > _bstr_t asdff(strv.c_str());
> > cout << (const wchar_t *)asdff;
> >
> >Jon
> >
> >
> >"Wayne Mack" <wmack@pec.com_nospam> wrote in message
> >news:39cf3825$1@news.devx.com...
> >>
> >> You can't cast to a BSTR, but you can construct a _bstr_t wrapper from
> a
> >const
> >> char *. Then you can extract the BSTR with const wchar_t *.
> >>
> >> _bstr_t asdf(strv.c_str());
> >> cout << (const wchar_t *)asdf;
> >>
> >> Note that std::cout does not take a BSTR and that a BSTR is not the
same
> >> structure as a wchar_t *, even though the compiler treats them the
same.
> >>
> >>
> >
> >
>
> b/c you are not using the unicode version of cout (wcout) you are
inserting
> the value of the ptr into the output stream, not the value being pointed
> to.
>
> try this:
>
> int main()
> {
> std::ostringstream os;
> os << "Hello World!" << endl;
> string strv = os.str();
> _bstr_t asdff(strv.c_str());
> wcout << static_cast<const wchar_t*>(asdff);
> return 0;
> }
>