Solved

How to convert BSTR to char*

Posted on 2002-04-03
6
3,609 Views
Last Modified: 2013-11-20
How to convert BSTR to char*
0
Comment
Question by:goodami
[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
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
Technology Partners: 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!

 
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 20 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

Three Considerations for Containers

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read our article on Experts Exchange.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

631 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