[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Print a BSTR

Posted on 2004-10-27
5
Medium Priority
?
2,221 Views
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.
0
Comment
Question by:jimstar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 4

Author Comment

by:jimstar
ID: 12430952
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
 
LVL 3

Accepted Solution

by:
Egore earned 500 total points
ID: 12435557
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
 
LVL 3

Expert Comment

by:Egore
ID: 12435686
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
 
LVL 4

Author Comment

by:jimstar
ID: 12436186
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
 
LVL 3

Expert Comment

by:Egore
ID: 12436392
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

656 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