convert BSTR * to LPCWSTR

Hello,
I am want to know how to convert BSTR *  to LPCWSTR.
Thanks.
SohelaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

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
drichardsCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.