Print a BSTR

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.
Who is Participating?
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);
jimstarAuthor Commented:
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.
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.
jimstarAuthor Commented:
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!
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.