Solved

Understanding problem

Posted on 2003-10-30
4
190 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 250 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

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 52
Dell Alienware Graphics Amplifier Driver for non-Alienware systems 18 472
"Black Box" Testing of Control System Software 2 94
Problem to App source 6 92
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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