Converting BSTR to string and vice versa

Posted on 2006-06-07
Medium Priority
Last Modified: 2008-01-09
Could anyone tell me how to convert BSTR to string and vice versa?
Also, how can I convert between BSTR and CString?
Question by:csound
  • 2
LVL 48

Assisted Solution

AlexFM earned 200 total points
ID: 16850137
You can use _bstr_t class. _bstr_t instance can be created from BSTR pointer, and casted to char* and wchar_t*.

void Foo(BSTR bstr)
    _bstr_t b(bstr, false);

    const char* pChar = (const char*)b;
    const wchar_t* pWChar = (const wchar_t*)b;

CString can be created from char*/wchar_t*. If BSTR is kept in VARIANT, you can use const VARIANT& constructor.
LVL 15

Accepted Solution

lakshman_ce earned 240 total points
ID: 16855562
1. You can convert at CString to a BSTR by calling the CString method AllocSysString:
CString s;
s = ... ; // whatever
BSTR b = s.AllocSysString();
 The pointer b points to a newly-allocated BSTR object which is a copy of the CString, including the terminal NUL character. This may now be passed to whatever interface you are calling that requires a BSTR. Normally, a BSTR is disposed of by the component receiving it. If you should need to dispose of a BSTR, you must use the call
to free the string.

2. For BSTR to CString you can also refer to this link

LVL 48

Expert Comment

ID: 17026114
Without questioner's reply, I suggest split.

Author Comment

ID: 17026417
I'm sorry for such a late reply. Thank you all for your helps.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question