Char * to LPWSTR

M

MattWilson.6185

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.pTextureFilename)];
strcpy(convertMe,materials.pTextureFilename);
wchar_t fileNameBuff[1];
int buffSize =
MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),fileNameBuff,0);
LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),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 :)
 
R

Ron Natalie

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
 
M

MattWilson.6185

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.pTextureFilename);
wchar_t fileNameBuff[1];
int buffSize
=MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),fileNameBuff,0);
LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),gah,buffSize);
/*LOOK HERE!!*/
wstring final;
for(int i = 0; i < sizeof(convertMe)*2+1;i++) {
final.push_back(gah);

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
 
D

Dag Viken

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
 
J

Jack Klein

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.pTextureFilename)];
strcpy(convertMe,materials.pTextureFilename);
wchar_t fileNameBuff[1];
int buffSize =
MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),fileNameBuff,0);
LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),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
 
T

Tim D

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.pTextureFilename);
wchar_t fileNameBuff[1];
int buffSize
=MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),fileNameBuff,0);
LPWSTR gah = (LPWSTR)new wchar_t[buffSize];
MultiByteToWideChar(CP_ACP,0,convertMe,strlen(convertMe),gah,buffSize);
/*LOOK HERE!!*/
wstring final;
for(int i = 0; i < sizeof(convertMe)*2+1;i++) {
final.push_back(gah);

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.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.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top