How to convert *string to _bstr_t and *BSTR
-
Hi, I'm creating dll using VS2005 ATL project. I'm using third party SDK API's which extracts data into a *string variable. Now I'm facing problem with converting the *string to _bstr_t and *BSTR. Please guide me whether is it possible to convert *string to _bstr_t and *BSTR, if so how to do it. If possible with a sample piece of code. thanking you, Arun
-
Hi, I'm creating dll using VS2005 ATL project. I'm using third party SDK API's which extracts data into a *string variable. Now I'm facing problem with converting the *string to _bstr_t and *BSTR. Please guide me whether is it possible to convert *string to _bstr_t and *BSTR, if so how to do it. If possible with a sample piece of code. thanking you, Arun
Hello agarunk, By "*string", do you mean a pointer to a STL string ? By "*BSTR", do you mean a pointer to a BSTR ? - Bio.
-
Hello agarunk, By "*string", do you mean a pointer to a STL string ? By "*BSTR", do you mean a pointer to a BSTR ? - Bio.
-
Hi Bio, Its "
std::string*
", i.e., pointer to std string. and yes "BSTR*
" is pointer to a BSTR. -ArunBoth the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't. You will need to use the c_str() member function of the std::string to get your LPCSTR in the first place (unlike MFC/ATL/WTL CString class, there's no LPCTSTR operator). Like I say, you can then use this pointer with something like A2W or MultiByteToWideChar.
Steve S Developer for hire
-
Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't. You will need to use the c_str() member function of the std::string to get your LPCSTR in the first place (unlike MFC/ATL/WTL CString class, there's no LPCTSTR operator). Like I say, you can then use this pointer with something like A2W or MultiByteToWideChar.
Steve S Developer for hire
Hi Steve, Below is the implementation which I'm using now....
BSTR* TargetBSTR ; string *SourceStr=new string; string StrBuff; const char *CharBuff; StrBuff=*SourceStr; CharBuff=StrBuff.c_str(); *TargetBSTR = _com_util::ConvertStringToBSTR(CharBuff);
Is this what you are asking me to do??? It is working fine for me for time being... I would like to know if I'm doing any mistake. Is there any issues with it??? -Arun -
Hi, I'm creating dll using VS2005 ATL project. I'm using third party SDK API's which extracts data into a *string variable. Now I'm facing problem with converting the *string to _bstr_t and *BSTR. Please guide me whether is it possible to convert *string to _bstr_t and *BSTR, if so how to do it. If possible with a sample piece of code. thanking you, Arun
Check out The Complete Guide to C++ Strings, Part II - String Wrapper Classes[^]
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't. You will need to use the c_str() member function of the std::string to get your LPCSTR in the first place (unlike MFC/ATL/WTL CString class, there's no LPCTSTR operator). Like I say, you can then use this pointer with something like A2W or MultiByteToWideChar.
Steve S Developer for hire
Steve S wrote:
Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't.
This isn't completely accurate.
_bstr_t
has a constructor that takes aconst char*
. It will do the conversion for you. Basically, if you need to convertstd::string*
to a_bstr_t*
(or even to aBSTR*
), here is a simple way to do it:std::string* pStlString = new string("my stuff"); _bstr_t* pBstrWrapper = new _bstr_t(pStlString->c_str()); BSTR* pBstr = pBstrWrapper->GetAddress();
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Hi Steve, Below is the implementation which I'm using now....
BSTR* TargetBSTR ; string *SourceStr=new string; string StrBuff; const char *CharBuff; StrBuff=*SourceStr; CharBuff=StrBuff.c_str(); *TargetBSTR = _com_util::ConvertStringToBSTR(CharBuff);
Is this what you are asking me to do??? It is working fine for me for time being... I would like to know if I'm doing any mistake. Is there any issues with it??? -Arunagarunk wrote:
BSTR* TargetBSTR ; string *SourceStr=new string; string StrBuff; const char *CharBuff; StrBuff=*SourceStr; CharBuff=StrBuff.c_str(); *TargetBSTR = _com_util::ConvertStringToBSTR(CharBuff);
That is far more code than you need to do for this. See my response below.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Steve S wrote:
Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't.
This isn't completely accurate.
_bstr_t
has a constructor that takes aconst char*
. It will do the conversion for you. Basically, if you need to convertstd::string*
to a_bstr_t*
(or even to aBSTR*
), here is a simple way to do it:std::string* pStlString = new string("my stuff"); _bstr_t* pBstrWrapper = new _bstr_t(pStlString->c_str()); BSTR* pBstr = pBstrWrapper->GetAddress();
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
I sit corrected :) I confess that these days I very rarely use ANSI/MBCS, since I no longer do stuff for Win9x/ME platforms. Lucky me, my end-customers all use W2K or later...
Steve S Developer for hire
Since I'm in the Linux world at my new job, I'm a bit rusty myself. I do find that using the T-functions (along with classes that can use both char* and wchar_t*) alleviates much of those headaches.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac