Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Char * to LPWSTR

Reply
Thread Tools

Char * to LPWSTR

 
 
MattWilson.6185@gmail.com
Guest
Posts: n/a
 
      11-25-2006
Hello!

I am trying to convert a char * to a LPWSTR, and I am going absolutly
mad! I can't find anything besides typle L"string" Unfortunetaly I
can't use that...

basicaly the setup is

callbackFromAnotherFunctionWhichMustBeOfChar(char* helpMe) {
//...
foo(some LPWSTR conversion);
}

this is the closest I have come to acomplishing this

char* convertMe = new char[sizeof(materials[i].pTextureFilename)];
strcpy(convertMe,materials[i].pTextureFilename);
wchar_t fileNameBuff[1];
int buffSize =
MultiByteToWideChar(CP_ACP,0,convertMe,strlen(conv ertMe),fileNameBuff,0);
LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
MultiByteToWideChar(CP_ACP,0,convertMe,strlen(conv ertMe),gah,buffSize);

it converts all of the characters but I get a bunch of junk at the end
and I can't open the file.

Thanks

 
Reply With Quote
 
 
 
 
Martijn van Buul
Guest
Posts: n/a
 
      11-25-2006
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> callbackFromAnotherFunctionWhichMustBeOfChar(char* helpMe) {
> //...
> foo(some LPWSTR conversion);
> }
>


Terribly off-topic here as it's not about C++, but about stupid MFC/ATL, but
you might want to peek at i

http://msdn2.microsoft.com/en-us/lib...a3(VS.80).aspx

--
Martijn van Buul - (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      11-25-2006
(E-Mail Removed) wrote:
> Hello!
>
> I am trying to convert a char * to a LPWSTR, and I am going absolutly
> mad! I can't find anything besides typle L"string" Unfortunetaly I
> can't use that...
>

L"string" is an array of wchar_t characters.
The topical question is how to change an array of
char to an array of wchar_t.

char narrowarray[] = "Mairzy doats and dozy doats.";
wchar_t widearray[100]
mbstowcs(widearray, narrowarray, 100);

LPSTR is a Microsoft abomination
 
Reply With Quote
 
MattWilson.6185@gmail.com
Guest
Posts: n/a
 
      11-25-2006
> L"string" is an array of wchar_t characters.
> The topical question is how to change an array of
> char to an array of wchar_t.
>
> char narrowarray[] = "Mairzy doats and dozy doats.";
> wchar_t widearray[100]
> mbstowcs(widearray, narrowarray, 100);
>
> LPSTR is a Microsoft abomination


Here Here! Thank you so much for the answer! I now have a curiosity
question if any are willing to indulge, to solve the problem i had
earlier I used

strcpy(convertMe,materials[i].pTextureFilename);
wchar_t fileNameBuff[1];
int buffSize
=MultiByteToWideChar(CP_ACP,0,convertMe,strlen(con vertMe),fileNameBuff,0);
LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
MultiByteToWideChar(CP_ACP,0,convertMe,strlen(conv ertMe),gah,buffSize);
/*LOOK HERE!!*/
wstring final;
for(int i = 0; i < sizeof(convertMe)*2+1;i++) {
final.push_back(gah[i]);

Can anyone tell me, why did I have to use a size of 2 + 1? It makes NO
sence at all to me, I mean I understand a wchar is twice as many bytes,
however in traversing the array does it access each part per byte
(char)? And if so then how does push_back understand it's getting char
and not wchar? Even when it's clearly being fed a wchar... I should
be able to say final.push_back(L"w"); not final.push_back"(L"w");
final.push_back(0); or whatever it be

thanks

Matt

 
Reply With Quote
 
Dag Viken
Guest
Posts: n/a
 
      11-25-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello!
>
> I am trying to convert a char * to a LPWSTR, and I am going absolutly
> mad! I can't find anything besides typle L"string" Unfortunetaly I
> can't use that...
>
> basicaly the setup is
>
> callbackFromAnotherFunctionWhichMustBeOfChar(char* helpMe) {
> //...
> foo(some LPWSTR conversion);
> }


Matt,
You should probably post this to: microsoft.public.vc.language

Your problem is that you are not copying the terminating null.
Try this :

int buffSize = MultiByteToWideChar(CP_ACP, 0, convertMe,
strlen(convertMe)+1, NULL, 0);
LPWSTR gah = new wchar_t[buffSize];
MultiByteToWideChar(CP_ACP, 0, convertMe, strlen(convertMe)+1, gah,
buffSize);

or, even easier:

int buffSize = (int)strlen(convertMe) + 1;
LPWSTR gah = new wchar_t[buffSize];
MultiByteToWideChar(CP_ACP, 0, convertMe, buffSize, gah, buffSize);

Now gah will have the null-terminated Unicode string.

Dag


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      11-27-2006
On 25 Nov 2006 07:54:51 -0800, (E-Mail Removed) wrote in
comp.lang.c++:

> Hello!
>
> I am trying to convert a char * to a LPWSTR, and I am going absolutly
> mad! I can't find anything besides typle L"string" Unfortunetaly I
> can't use that...
>
> basicaly the setup is
>
> callbackFromAnotherFunctionWhichMustBeOfChar(char* helpMe) {
> //...
> foo(some LPWSTR conversion);
> }
>
> this is the closest I have come to acomplishing this
>
> char* convertMe = new char[sizeof(materials[i].pTextureFilename)];
> strcpy(convertMe,materials[i].pTextureFilename);
> wchar_t fileNameBuff[1];
> int buffSize =
> MultiByteToWideChar(CP_ACP,0,convertMe,strlen(conv ertMe),fileNameBuff,0);
> LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
> MultiByteToWideChar(CP_ACP,0,convertMe,strlen(conv ertMe),gah,buffSize);
>
> it converts all of the characters but I get a bunch of junk at the end
> and I can't open the file.
>
> Thanks


You say you can't use standard C++ features, and your code uses
non-standard, Windows specific types and API functions, all off-topic
here.

You need to ask this in a Windows group, such as
news:comp.os.ms-windows.programmer.win32.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Tim D
Guest
Posts: n/a
 
      11-28-2006
(E-Mail Removed) wrote:

>> L"string" is an array of wchar_t characters.
>> The topical question is how to change an array of
>> char to an array of wchar_t.
>>
>> char narrowarray[] = "Mairzy doats and dozy doats.";
>> wchar_t widearray[100]
>> mbstowcs(widearray, narrowarray, 100);
>>
>> LPSTR is a Microsoft abomination

>
> Here Here! Thank you so much for the answer! I now have a curiosity
> question if any are willing to indulge, to solve the problem i had
> earlier I used
>
> strcpy(convertMe,materials[i].pTextureFilename);
> wchar_t fileNameBuff[1];
> int buffSize
> =MultiByteToWideChar(CP_ACP,0,convertMe,strlen(con vertMe),fileNameBuff,0);
> LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
> MultiByteToWideChar(CP_ACP,0,convertMe,strlen(conv ertMe),gah,buffSize);
> /*LOOK HERE!!*/
> wstring final;
> for(int i = 0; i < sizeof(convertMe)*2+1;i++) {
> final.push_back(gah[i]);
>
> Can anyone tell me, why did I have to use a size of 2 + 1? It makes NO
> sence at all to me, I mean I understand a wchar is twice as many bytes,
> however in traversing the array does it access each part per byte
> (char)? And if so then how does push_back understand it's getting char
> and not wchar? Even when it's clearly being fed a wchar... I should
> be able to say final.push_back(L"w"); not final.push_back"(L"w");
> final.push_back(0); or whatever it be
>
> thanks
>
> Matt


MultiByteToWideChar does not return a nul terminated C-style string, just
the converted characters. If you want a C-style string, you need to do the
bookkeeping yourself:

int buffSize = MultiByteToWideChar(
CP_ACP,
0,
materials[i].pTextureFilename, // no need to copy the src string
-1, // -1 means src string is nul terminated
NULL, // not used when calc'ing size
0);
wchar_t gah = new wchar_t[buffSize + 1]; // translated chars plus trailing
nul
MultiByteToWideChar(
CP_ACP,
0,
materials[i].pTextureFilename,
-1,
gah,
buffSize);
gah[buffSize] = 0; // make it a C-string

Suppose you did something like this:

char fred[]="fred";
char* newfred = new char[strlen(fred)];
strcpy(newfred,fred);

You'd blow up because you neglected to include the extra nul that strcpy
puts on the end of the string. With MultiByteToWideChar, you need to do the
strcpy-like bookkeeping yourself.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically allocate array variable of type LPWSTR Trups C Programming 10 11-04-2008 08:13 AM
dynamically allocate array variable type LPWSTR Samant.Trupti@gmail.com C++ 1 10-31-2008 04:33 PM
Converting LPWSTR to LPCWSTR dehi@gmx.de C++ 2 02-26-2007 01:11 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments