Converting BSTR to string and vice versa

Posted on 2006-06-07
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
    LVL 48

    Assisted Solution

    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

    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

    Without questioner's reply, I suggest split.

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now