• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2259
  • Last Modified:

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.
0
jimstar
Asked:
jimstar
  • 3
  • 2
1 Solution
 
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.
0
 
EgoreCommented:
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;

USES_CONVERSION;
// Convert the display_text variable to a char array
text = new char[SysStringByteLen(display_text)];
text = OLE2T(display_text);
0
 
EgoreCommented:
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.
0
 
jimstarAuthor Commented:
When I add the _bstr_t line, I get:

Compiling...
<file>.cpp
Linking...
<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!
0
 
EgoreCommented:
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.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now