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

Converting from _bstr_t to char* or std::string

3,061 views
Skip to first unread message

Peter Olcott

unread,
Jan 20, 2007, 1:46:32 PM1/20/07
to
What is the best way to convert from _bstr_t to char*?


Peter Olcott

unread,
Jan 20, 2007, 2:03:16 PM1/20/07
to
"Peter Olcott" <NoS...@SeeScreen.com> wrote in message
news:9stsh.65200$kn7....@newsfe23.lga...

> What is the best way to convert from _bstr_t to char*?
>
I found this just after I posted this message:

_bstr_t BString;
char String[100];
BString = "Hello World!"; // automatic conversion
strcpy(String, (char*)BString); // uses operator const char*( ) const throw(
_com_error )

std::string stdString;
stdString = (char*)BString;

Wyvern

unread,
Jan 20, 2007, 3:35:51 PM1/20/07
to
Hello Peter,


BSTR bstrText = ::SysAllocString(L"Test");
char* szText = _com_util::ConvertBSTRToString(bstrText);
::SysFreeString(bstrText);

Take it easy.


Ajay Kalra

unread,
Jan 20, 2007, 10:44:25 PM1/20/07
to
Also, if you are using MBCS, CString will take a CComBSTR in the constructor
directly.

--
Ajay Kalra [MVP - VC++]
ajay...@yahoo.com


"Peter Olcott" <NoS...@SeeScreen.com> wrote in message

news:7Itsh.65205$kn7....@newsfe23.lga...

Nick Burkitt

unread,
Mar 13, 2007, 9:01:16 PM3/13/07
to
I think this approach will leak memory.
The method "_bstr_t::operator const char*() const" calls "const char*
_bstr_t::Data_t::GetString() const" which calls
"_com_util::ConvertBSTRToString()", which returns a string that must be freed
by a call to delete[].
You probaby want to do something like

bstr_t BString;
char String[100];
BString = "Hello World!";

char* temp = (char*)BString;
strcpy(String, temp);
delete temp[];

and

std::string stdString;
char* temp = (char*)BString;
stdString = temp;
delete temp[];

-Nick

FJ

unread,
Mar 14, 2007, 8:20:25 AM3/14/07
to
Peter Olcott wrote:
> "Peter Olcott" <NoS...@SeeScreen.com> wrote in message
> news:9stsh.65200$kn7....@newsfe23.lga...
>> What is the best way to convert from _bstr_t to char*?
>>
> I found this just after I posted this message:
>
> _bstr_t BString;
> char String[100];
> BString = "Hello World!"; // automatic conversion

You can just call the coversion constructor directly:

_bstr_t bstring( "Hello World" );

> strcpy(String, (char*)BString); // uses operator const char*( )

I'm not sure what you're wanting to do with the above, but it's not needed
for the conversion to std::string

> const throw( _com_error )


This is invalid code in this context.

> std::string stdString;
> stdString = (char*)BString;

You'll want to use _bstr_t::operator const char*() here. And it's best to
call the conversion constructor directly as in either of the two statements:

std::string stdString( (const char*)bstring );
std::string stdString = (const char*)bstring;

I'm not sure if BSTR's can have embedded nulls. If so you'll need to use:

std::string stdString( (const char*)bstring, bstring.length() );

Jeff Flinn


0 new messages