Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Understanding problem

Posted on 2003-10-30
4
Medium Priority
?
193 Views
Last Modified: 2013-11-20
Hi all

I am having a small problem. I have written a piece of code and want to find what exactly it will react as.

The code is as follows.
BSTR      bstr ;
CString str="hello this is a test" ;
bstr = str.AllocSysString() ;
AfxMessageBox((LPCTSTR)(_bstr_t)bst);

I wanted to show a message box or u can say wanted to extract the data in the BSTR variable without using a _bstr_t or CString avriable.
So i tried this stunt i.e AfxMessageBox((LPCTSTR)(_bstr_t)bstr);
Please tell me if this will work only as a typecast operator , or it will allocate memory for the BSTR to _bstr_t conversion.

Thanks a lot.
0
Comment
Question by:sudeshsh
[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
4 Comments
 
LVL 3

Expert Comment

by:freewell
ID: 9654927
#include "afxpriv.h"

BOOL CAboutDlg::OnInitDialog()
{
      CDialog::OnInitDialog();
      // TODO: Add extra initialization here

      USES_CONVERSION;
      CString str1("Test Conversion");
      BSTR bstr = T2OLE(str1);
      AfxMessageBox(OLE2T(bstr));

      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}
0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9656105
_bstr_t is an object, not a native type, so you are actually calling the _bstr_t constructor, which will indeed allocate memory for the _bstr_t object. Space will also be allocated to store the string itself.

Hope this helps
Martyn
0
 
LVL 4

Expert Comment

by:inox
ID: 9658286

BSTR is a WCHAR String

AfxMessageBox is a 2 version (by param) function

if you create a none unicode version space must be allocated for the conversion to ASCII

in a unicodeversion you can just write
AfxMessageBox(bstr)

in a ansi/asciiversion you can use
MessageBoxW(0,bstr,L"whatever",MB_OK);
or other ...W API calls w/o ascii conversion (allocation)
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 750 total points
ID: 9664417
Your question is somwhat unclear.  It seems that you just want to "look at" the text that is in a BSTR.   If so, just place a breakpoint in the code, and then place the mouse over the variable named bstr  -- the current value will appear in a little toottip-like box.

Some background:  
BSTR is just a pointer data type like char* and the compiler/debugger knows how to display them.

Sometimes (even often) you will need to convert to and from a BSTR to a char* -- and that will involve some process that looks at each character in the one array and converting it to the desired type in the other array.

The simplest way to get that conversion done is to use the _bstr_t class object, as you have already done.  _bstr_t is kind of an interesting object:  It actually maintains two copies of the text, one in Wide UNICODE form and one in 8-bit char form.  It provides conversion operators that let you examine the text in either form.

=-=-=-=-=-=-=-=-=-=-=
If that does not help, then please supply some more information about what you are trying to accomplish or what else you need to know.

-- Dan
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

636 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