Solved

Quickest way from BSTR to char * (no mfc)

Posted on 2001-06-13
20
1,397 Views
Last Modified: 2013-11-25
Using ATL alone, what is the quickest way to convert a BSTR * to/from a char * ?

I want to pass a BSTR * as a parameter, and also return them.

Eg.
[id(3)] HRESULT Test_String([in] BSTR* szInString, [out, retval] BSTR *szRetval);

I'm using strtok internally, so unless there's a BSTR equivalent, then I need to use char *.  Any thoughts?
0
Comment
Question by:InetMatt
  • 4
  • 4
  • 2
  • +9
20 Comments
 
LVL 2

Expert Comment

by:GloriousRain
Comment Utility
BSTR bstrText("Sample");
char lpText[256];
strcpy(lpText, (LPSTR)_bstr_t(bstrText));
0
 

Expert Comment

by:RanganathanVenkatakrishnan
Comment Utility
1. VarI1FromStr
This function converts variant data types to char from BSTR.

2. VarBstrFromI1
This function converts variant data types to BSTR from char.

// I hope this solves your problem.

0
 
LVL 2

Expert Comment

by:GloriousRain
Comment Utility
oop, _bstr_t bstrText("Sample") instead of BSTR bstrText("Sample")
0
 

Author Comment

by:InetMatt
Comment Utility
Ok, that's great, but I think I need code samples.  I've found heaps of little bits that almost do the job, but failures are really annoying me.
0
 

Author Comment

by:InetMatt
Comment Utility
What I've noticed is that there are about a billion ways to do this, and I want to find the easiest and most efficient way of doing this.  
0
 

Author Comment

by:InetMatt
Comment Utility
A little more detail please?
0
 
LVL 22

Accepted Solution

by:
ambience earned 75 total points
Comment Utility
//From TCHAR* to BSTR
bool CAccountInformationImpl::GetUserID(BSTR *pVal)
{
     _bstr_t  tempData ( _T("MY_USER") ); //
     *pVal = tempData.copy();
     return true;
}


// From BSTR to TCHAR*
bool CAccountInformationImpl::SetUserID(BSTR newVal)
{
     // TODO: Add your implementation code here
     _bstr_t  tempData ( newVal, FALSE);
     _tcscpy ( m_UserID, tempData );
     return true;
}

maybe this should help ...
0
 
LVL 22

Expert Comment

by:ambience
Comment Utility
m_USerID is defined as

TCHAR m_UserID [ 256 ];
0
 

Author Comment

by:InetMatt
Comment Utility
Are you sure that you have to convert BSTR -> _bstr_t -> TCHAR?  isn't there a quicker way?  

Eg.
BSTR -> TCHAR
0
 

Expert Comment

by:NickAtBOM
Comment Utility
Use the ALT string conversion macros.

Somewhere in your function, before the conversion you will need the line:

USES_CONVERSION;    // this macro expands up to variable
                    // definitions

Then, to convert to a BSTR from a char use:

strBSTR = A2OLE(strChar);

and to convert back use

strChar = OLE2A(strBSTR);

check out http://msdn.microsoft.com/library/devprods/vs6/visualc/vcmfc/_atl_string_conversion_macros.htm for a full discussion.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 6

Expert Comment

by:snoegler
Comment Utility
LPWSTR ToWideString(LPCTSTR lpszSource)
{
#ifndef _UNICODE
  int len;
  LPWSTR lpwszResult = NULL;
  len = MultiByteToWideChar(CP_ACP,0,lpszSourceString,-1,NULL,0);
  lpwszResult = new WCHAR[len+1];
  MultiByteToWideChar(CP_ACP,0,lpszSourceString,-1,lpwszResult,len);
  return lpwResult;
#else
  LPWSTR lpwszResult = new WCHAR[_wcslen(lpszSource)+1];
  _wcscpy(lpwszResult,lpszSource);
  return lpszSource;
#endif
}

LPCTSTR ToTString(LPWSTR lpwszSource)
{
#ifndef _UNICODE
  int lenBytes;
  LPTSTR lpszResult = NULL;
  lenBytes = WideCharToMultiByte(CP_ACP,0,lpwszSource,-1,NULL,0,NULL,NULL);
  lpszResult = new TCHAR[(lenBytes/sizeof(TCHAR))+1];
  WideCharToMultiByte(CP_ACP,0,lpwszSource,-1,lpszResult,lenBytes,NULL,NULL);
  return lpszResult;
#else
  LPWSTR lpwszResult = new WCHAR[_wcslen(lpszSource)+1];
  _wcscpy(lpwszResult,lpszSource);
  return lpszSource;
#endif
}

BSTR ToBSTR(LPCTSTR lpszSource)
{
  LPWSTR lpwszSource = ToWideChar(lpszSource);
  BSTR bstrRes = SysAllocString(lpwszSource);
  delete[] lpwszSource;
  return bstrRes;
}

I hope those work, i didn't try them yet.
Note that you can use ToTString also with a BSTR argument, there is no need for something like BSTRToTString!
0
 
LVL 9

Expert Comment

by:jasonclarke
Comment Utility
As mentioned before, the _bstr_t is the simplest form to use:

STDMETHODIMP CTest::Test(BSTR istr, BSTR *ostr)
{
    _bstr_t bs(istr);

    const char* s = static_cast<const char*>(bs);

    _bstr_t os(s);

    *ostr = os.copy();

    return S_OK;
}

0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
inetmatt Date: 06/14/2001 12:44AM PST
>> Are you sure that you have to convert BSTR -> _bstr_t -> TCHAR?  isn't there a quicker way?  

0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
>> there is no need for something like BSTRToTString!
As long as you are using BSTR's not containing '\0' characters, i forgot; but i guess that's not the case here ...
0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
If you need it even faster and you don't worry about codepage problems and localization (can be dangerous), you can also convert a TCHAR string to a WCHAR string like this:

LPWSTR lpwszResult = new WCHAR[_tcslen(lpszSource)+1];
int i = 0;
for(int i=0;(lpwszResult[i]=lpszSource[i])!='\0';i++) {};

But you're going to run into problems when it happens that the BSTR really contains unicode characters ...
Converting the WCHAR* to a BSTR can then be done using SysAllocString.
0
 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
> id(3)] HRESULT Test_String([in] BSTR* szInString, [out, retval] BSTR *szRetval);

on an aside this should be (note I removed the * from the [in] parameter)

id(3)] HRESULT Test_String([in] BSTR szInString, [out, retval] BSTR *szRetval);

as to conversions - if you are on NT only then use unicode at all times then you can treat a BSTR as WCHAR for most of the time (but you cannot treat WCHAR as BSTRs) and will require no conversions - so the quickest conversion is no conversion - else if you have to use ansi then use the ATL macros - howver make sure you use the right ones

eg A2OLE does not create BSTR that can be used in COM calls as it is the same as A2W you need to use A2BSTR however you need to be very careful

bad

pMyObj->MyFunc(A2BSTR("data")); // this will leak

good

BSTR bstrData=A2BSTR("data");
pMyObj->MyFunc(bstrData);
::SysFreeString(bstrData);

this is why you will see people use wrapper classes such as CComBSTR and _bstr_t as it save on the typing - however the above is quicker as it does not have to create the CComBSTR (or _bstr_t) object. however even the wrapper classes have bugs due to their dependancies on the ATL classes see KB Q241857


0
 

Expert Comment

by:balugaa
Comment Utility
I dont thik u can get easier than

#include <comutil.h>

using namespace _com_util
{
BSTR ConvertStringToBSTR(const char* pSrc) throw(_com_error);

     // Convert BSTR to char *
     //
char* __stdcall ConvertBSTRToString(BSTR pSrc) throw(_com_error);
}

these two utility functions work a treat

good luck

....
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
InetMatt,

Did you get your answer.  If so please, please award points.
If not, please request specific information.

Thanks
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "ambience" comment(s) as an answer. This was the first answer to solve the problem, it is hard to evaluate the request for a 'quicker way'.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

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.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

744 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

17 Experts available now in Live!

Get 1:1 Help Now