convert BSTR * to LPCWSTR

Hello,
I am want to know how to convert BSTR *  to LPCWSTR.
Thanks.
SohelaAsked:
Who is Participating?
 
drichardsConnect With a Mentor Commented:
Ah, my mistake.  You have a BSTR*, which is like wchar**.  You can't cast that to LPCWSTR.

You should use just BSTR, or you need W2A(*szCurRev);

Also, szCurRev is a bad name (wrong prefix) for a BSTR.

And finally, if you use _bstr_t you don't need W2A.

    _bstr_t bst("Hi there");
    std::string s = bst;

0
 
peprCommented:
Use the _bstr_t class (http://msdn.microsoft.com/en-us/library/zthfhkd6(VS.71).aspx) and initialize it with your BSTR using one of the constructors. Then use operator wchar_t* operator for extracting the pointer (http://msdn.microsoft.com/en-us/library/btdzb8eb(VS.71).aspx).

See also some information at http://www.codeguru.com/cpp/cpp/string/conversions/article.php/c5639
0
 
drichardsCommented:
You can just cast it.  A BSTR is just a pointer to  unicode string, but the implementation is that there is a length in the 4 bytes ahead of the pointer value.  That's why there are special functions to create and destroy them.  Looka the the implementstion of _bstr_t in comutil.h for details.  In fact, if you use the _bstr_t class, it has a built-in cast operator to wchar* const and const wchar* const.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
SohelaAuthor Commented:
Thanks for the reply but im not getting it here is my code
BSTR *szCurRev;
std::string szCurRev1 = W2A (szCurRev);

when i am executing the following error is coming

cannot convert from 'BSTR * ' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
0
 
drichardsCommented:
One note, however, the reason BSTR's have the length out front os so they can support embedded null characters - they are not null-terminated.  This meaens that standard string functions may not work correctly.  Life is good as long as there are not null characaters in the middle of teh strings.
0
 
drichardsCommented:
>> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

True, but it is OK.  That's what _bstr_t does.
0
 
SohelaAuthor Commented:
It is still showing the same error cannot convert from _bstr_t to lpcwstr
0
 
SohelaAuthor Commented:
>>>>need W2A(*szCurRev);
hey thanks alot im using this and it is coming
0
 
peprCommented:
However, W2A means wide to ASCII (better to say to 8bit encoding]. This way you are loosing the Unicode and sometimes the conversion may fail. If it does not fail (conversion to 8bit successful) then you wil not get LPCWSTR, but char*. It will seemingly work because another function will be used based on a different type of the passed argument. However, you have just killed the Unicode.

Also, std::string uses 8bit encoding.
0
 
SohelaAuthor Commented:
>>>>>>>However, W2A means wide to ASCII (better to say to 8bit encoding]. This way you are loosing the Unicode and sometimes the conversion may fail. If it does not fail (conversion to 8bit successful) then you wil not get LPCWSTR, but char*. It will seemingly work because another function will be used based on a different type of the passed argument. However, you have just killed the Unicode.
Also, std::string uses 8bit encoding.


Then what should i do
0
 
peprCommented:
Try the attached sample. The code in the snippet.
#include <comutil.h>
#include <string>
 
using namespace std;
 
int wmain()
{
    _bstr_t bsEN(L"All the best\n");
    _bstr_t bsCN(L"@	}\n");
    _bstr_t bsRU(L"A5 A0<>5 ;CGH55\n");
 
	// You can directly use the _bstr_t objects...
    MessageBoxW(NULL, bsEN+bsCN+bsRU, L"Using _bstr_t", 0);
 
    wstring sEN(bsEN);
    wstring sCN(bsCN);
    wstring sRU(bsRU);
 
	// ... or you can convert them to wstring.
	wstring sAll(sEN+sCN+sRU);
	MessageBoxW(NULL, sAll.c_str(), L"using std::wstring", 0);
 
    return 0;
}

Open in new window

Bstr2Wstr.zip
0
 
peprCommented:
Sorry for the indentation -- mixing tabs and spaces.
0
 
SohelaAuthor Commented:
no problem thanks a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.