Solved

How to convert BSTR to char*

Posted on 2002-04-03
6
3,603 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
Industry Leaders: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 85
Detect CR LF to each line 12 185
How to know only "File created" with EventLog 3 35
fizzArray  challenge 1 115
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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