Solved

How to convert BSTR to char*

Posted on 2002-04-03
6
3,589 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
Comment Utility
#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
Comment Utility
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
Comment Utility
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 5

Expert Comment

by:Netminder
Comment Utility
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
Comment Utility
#include <comutil.h>

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

Accepted Solution

by:
codeskunk earned 20 total points
Comment Utility
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

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

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 (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

16 Experts available now in Live!

Get 1:1 Help Now