Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to convert BSTR to char*

Posted on 2002-04-03
6
Medium Priority
?
3,627 Views
Last Modified: 2013-11-20
How to convert BSTR to char*
0
Comment
Question by:goodami
6 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6915427
#include <comdef.h>
...

// Get the BSTR into a _bstr_t class
_bstr_t mybstr = new _bstr_t(YOUR_BSTR_VAR, false);

// Use the (char *) operator from _bstr_t to get this into
// a const char *
const char *lpszStr = (char *)mybstr;

// Do your char * string thing..

// Delete the _bstr_t
delete mybstr;
0
 
LVL 4

Expert Comment

by:mblat
ID: 6915685
or you can use

myFunc(BSTR bstr)
{
USES_CONVERSION;

    char* pOut = OLE2A(bstr);

}

I suggest you read topic on conversion macros (Microsoft TN059) , there are some points you need to now about memory menagement, to use them correctly.

Hope it helps.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6916110
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:Netminder
ID: 6916347
mblat,

Welcome to Experts Exchange!

It's common practice here to use the Comment button, rather than the Answer button, when responding to a question, unless you are absolutely 100 per cent certain that your response is the ONLY complete and accurate solution to a problem, AND that your response doesn't duplicate or paraphrase someone else's response. Because your suggestion does not fulfill those two criteria, I've rejected your answer.

EE is a collaborative website; a major part of what makes it work is that an Asker can get a variety of suggestions. When you Propose an Answer, the question gets Locked, which makes it less likely for that collaboration to occur.

The Asker sees a button which says "Accept This Comment As Answer" -- so if he/she chooses your response, you'll get the points.

For more information, there are tips at the bottom of the page, as well as the link suggested by jhance.

Again, welcome aboard,

Netminder
Community Support Moderator
0
 
LVL 3

Expert Comment

by:Crius
ID: 6918459
#include <comutil.h>

char *WorkCharString = _com_util::ConvertBSTRToString(InBSTR);
0
 
LVL 1

Accepted Solution

by:
codeskunk earned 60 total points
ID: 6919713
This IS the MFC forum... am I right ???
What happened to CString ???

Just assign the BSTR throuh operater= or pass through the  constructor.  Your string can now be cast as an LPCSTR (if you need it).

CString csString( TheBSTRToConvert );

The other way around... (In case you wondered)

SomeFunct( BSTR* pFillMe )
{
   CString csValue( "I am gonna fill a BSTR" );
   pFillMe = csValue.AllocSysString();
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Screencast - Getting to Know the Pipeline

782 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