[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

convert BSTR *  to LPCWSTR

Posted on 2008-11-06
13
Medium Priority
?
2,798 Views
Last Modified: 2012-05-05
Hello,
I am want to know how to convert BSTR *  to LPCWSTR.
Thanks.
0
Comment
Question by:Sohela
  • 5
  • 4
  • 4
13 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 22902555
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
 
LVL 19

Expert Comment

by:drichards
ID: 22902585
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
 

Author Comment

by:Sohela
ID: 22902588
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:drichards
ID: 22902589
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
 
LVL 19

Expert Comment

by:drichards
ID: 22902594
>> 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
 

Author Comment

by:Sohela
ID: 22902629
It is still showing the same error cannot convert from _bstr_t to lpcwstr
0
 
LVL 19

Accepted Solution

by:
drichards earned 2000 total points
ID: 22902726
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
 

Author Comment

by:Sohela
ID: 22902756
>>>>need W2A(*szCurRev);
hey thanks alot im using this and it is coming
0
 
LVL 29

Expert Comment

by:pepr
ID: 22903259
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
 

Author Comment

by:Sohela
ID: 22903330
>>>>>>>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
 
LVL 29

Expert Comment

by:pepr
ID: 22903842
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
 
LVL 29

Expert Comment

by:pepr
ID: 22903853
Sorry for the indentation -- mixing tabs and spaces.
0
 

Author Comment

by:Sohela
ID: 22903878
no problem thanks a lot
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

873 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