Print a BSTR

Posted on 2004-10-27
Last Modified: 2008-01-09
Can someone provide me code to print a BSTR to the screen from Visual Studio 6 (C) - I prefer to not use MFC.

I tried to use MessageBox(0, myBSTR, "title", MB_OK); but that doesn't work, as the types don't match.

Any code would be appreciated.  Thanks.
Question by:jimstar
    LVL 4

    Author Comment

    Code to convert a BSTR to a char* would work as well, since I don't use BSTR's normally, and I'll have to manipulate the BSTR later in my code.  If it were converted to a char*, that would help a lot.
    LVL 3

    Accepted Solution

    In a COM project you can use the following (assume that "display_text" is a BSTR):

    if( SysStringByteLen(display_text) == 0 )
          return S_OK;

    char *text;

    // Convert the display_text variable to a char array
    text = new char[SysStringByteLen(display_text)];
    text = OLE2T(display_text);
    LVL 3

    Expert Comment

    According to MSDN, there is another way that this can be done.  The _bstr_t data type can be converted to an LPCTSTR implicitely, so it should be valid to pass to MessageBox.  Try this code:

    BSTR tmpBStr;
    _bstr_t tmpbstr(tmpBStr, FALSE); //necessary to avoid a memory leak
    MessageBox(0, tmpbstr, "title", MB_OK);

    Just be warned that you must use the constructor for _bstr_t like it is written or you will cause a memory leak.  If you have the MSDN documentation, this is under the BSTR: Technical Articles heading.
    LVL 4

    Author Comment

    When I add the _bstr_t line, I get:

    <file>.obj : error LNK2001: unresolved external symbol "void __stdcall _com_issue_error(long)" (?_com_issue_error@@YGXJ@Z)
    Debug/<file>.dll : fatal error LNK1120: 1 unresolved externals

    However, before I put in that line, I had no errors.  I'm very new to COM/ATL, and have no idea how to troubleshoot this error.

    The first post above though works, so I'll give you the points for the question.  Thanks!
    LVL 3

    Expert Comment

    You're welcome!

    If you want to go the _bstr_t route, you may need to specify: #include <comdef.h>

    Just be warned that _bstr_t is Microsoft-specific.  I don't know about the other method, though.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    857 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

    13 Experts available now in Live!

    Get 1:1 Help Now