?
Solved

Understanding problem

Posted on 2003-10-30
4
Medium Priority
?
192 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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 Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

764 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