Click here to Skip to main content
15,918,617 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I am want to convert BYTE* to LPWSTR as below, which is not correct, Please advise correct mechanism to do so. Many Thanks in advance!!

C++
BYTE *bpString;// with some data
....
LPWSTR plstr = (LPWSTR)bpString;


What I have tried:

LPWSTR plstr = (LPWSTR)bpString

and also mbstowcs(), but did not succeeded
Posted
Updated 18-Feb-16 11:28am
Comments
Andreas Gieriet 18-Feb-16 13:36pm    
What means: "did not succeeded"?
What is the (compiler?) error message?
You seem to assume that one takes the time to reverse engineer your problem.
Help us to help you!
Andi
ptr_Electron 18-Feb-16 14:21pm    
Thanks for response.. no compilers errors. the line mbstowcs crashes, and also want to do dynamically depending on bpString size, instead of hard coding to 2000
Please advise. Thank you

wchar_t wSStr[2000]; //and also want to do dynamically
mbstowcs(wSStr, (const char*)bpString, bpStringLen+1);
LPWSTR lpwStr = wSStr;
Andreas Gieriet 18-Feb-16 15:03pm    
Have you read mbstowcs - C++ Reference?
What is bpString?
What is bpStringLen? This should be the length of the wSStr array, not of the source buffer.
Cheers
Andi
ptr_Electron 18-Feb-16 15:35pm    
Thank for responce.. but how can find the destination string length, and also could you please advise how to create wchar_t wSStr of dynamic length. Thanks

BYTE *bpString;
wchar_t wSStr[2000]; //and also want to do dynamically
mbstowcs(wSStr, (const char*)bpString, bpStringLen+1);
LPCWSTR lpwStr = wSStr;
Andreas Gieriet 18-Feb-16 17:20pm    
Have you read the documentation as I suggested? It's all there.
Your array of 2000 wchar_t has a length of 2000, so pass these 2000 to the respective parameter. Note that it will not be null-terminated after the conversion (see the documentation)!
You do not give in your code above the source string. Where the heck should the function find the source string from?
I'm a bit puzzled why you cannot figure out the semantics from the description...
Regards
Andi
PS: Why the cast? A cast is by definition a code smell that needs to be described! Have you tried without the cast to const char*? How is BYTE defined? Does it need to be BYTE? The function is meant to be used with char arrays.

1 solution

Have a look at the code sample provided by the mbstowcs man page[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900