?
Solved

How to convert BSTR to char*

Posted on 2002-04-03
6
Medium Priority
?
3,619 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

719 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