By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Char * to LPWSTR

P: n/a
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 :)

Nov 25 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* Ma*************@gmail.com:
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 - pi**@dohd.org
Nov 25 '06 #2

P: n/a
Ma*************@gmail.com 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
Nov 25 '06 #3

P: n/a
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

Nov 25 '06 #4

P: n/a
<Ma*************@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr 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
Nov 25 '06 #5

P: n/a
On 25 Nov 2006 07:54:51 -0800, Ma*************@gmail.com 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
Nov 27 '06 #6

P: n/a
Ma*************@gmail.com 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.
Nov 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.