Solved

Understanding problem

Posted on 2003-10-30
4
187 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
4 Comments
 
LVL 3

Expert Comment

by:freewell
Comment Utility
#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
Comment Utility
_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
Comment Utility

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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now