convert a BSTR data type to std::string

H

Howard

Karthik said:
Hello,

How can I convert a BSTR data type to std::string??? Thanks a much!

Karthik

Ask in a microsoft newsgroup. I believe BSTR is a microsoft-defined type.

(But, have you tried simply assigning from your BSTR variable to your
std::string variable, or creating a std::string with the BSTR as the
std::string's constructor's parameter?)

-Howard
 
H

Heinz Ozwirk

Karthik said:
How can I convert a BSTR data type to std::string??? Thanks a much!

A BSTR - if you are talking about the Microsoft thing - basically is a
unicode string. To convert it into an std::string you have to use functions
like wcstombs or WideCharToMultiByte. But you should better use std::wstring
instead of std::string, or you cannot convert all possible characters in a
BSTR.

HTH
Heinz
 
J

Jonathan Mcdougall

How can I convert a BSTR data type to std::string??? Thanks a much!

Seems a BSTR is a double-byte string, so you won't be able to put it in
a std::string. Use std::wstring instead. Something like

void f(BSTR ms_str)
{
std::wstring ws(ms_str);
}

should work, but you would be better served in a microsoft newgroup.

Jonathan
 
D

Default User

Howard said:
Ask in a microsoft newsgroup. I believe BSTR is a microsoft-defined type.

That's a good suggestion.
(But, have you tried simply assigning from your BSTR variable to your
std::string variable, or creating a std::string with the BSTR as the
std::string's constructor's parameter?)

I'm guessing that it probably won't work, based on what I read here:

http://www.codeproject.com/string/bstrsproject1.asp

This may help the OP.



Brian
 
P

Phlip

Karthik said:
How can I convert a BSTR data type to std::string??? Thanks a much!

#include <comdef.h>

std::string myString = _bstr_t (myBSTR);

Future BSTR questions belong on some other newsgroup blah blah blah...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top