Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type conversion: char to LPCWSTR

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Is there a way to convert a char (or CString) into LPCWSTR.
Thanx.
 
Have a look at MultiByteToWideChar() in Platform SDK

/JOlesen
 
Hi joleson

That does work but only for one value. What I mean is that if I have two different strings to be converted into two different LPCWSTR, both gets assigned the same value. Am I missing something in the implementation below:

// convert CString into unsigned short*
CString appName="Test App";
CString cwd="C://windows";

unsigned short* appNameLPCW;
unsigned short* cwdLPCW;

MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, appName, MAX_SIZE, appNameLPCW, MAX_SIZE);

MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, "cwd", MAX_SIZE, cwdLPCW, MAX_SIZE);
 
I use this function very much, so I know it works.

You need to allocate storage that these 2 pointers point to :
unsigned short* appNameLPCW;
unsigned short* cwdLPCW;

Also you should check the returncode from the MultiByteToWideChar().

What is MAX_SIZE ? One parameter should tell the size of the inpustring, and another should tell the size of the allocated output buffer. You use MAX_SIZE for both - and in both functioncalls - it must be wrong !.

/JOlesen
 
const int MAX_SIZE = 255 ......

I am still confused. Like I said above, the declaration is correct and it DOES work. But that is as long as I use only one of them.

I get the correct answer but the value of the one that is defined second is gets initialized in both the variables ie. if appName is defined first/used first.
then if the value of appNameLPCW = "C:/program files/test.exe";
Then the vlaue of cwdLPCW is also = "C:/program files/test.exe";


Can you post your code where you use MultiByteToWideChar simultaneously.

 
>I am still confused. Like I said above, the declaration is
>correct and it DOES work. But that is as long as I use
>only one of them.

Like JOlesen already said: You have not allocated any space for your output buffer (...LPCW), so the behaviour of MultiByteToWideChar() is undefined. Your implementation should be:

Code:
const int MAXSIZE = 512;

CString appName="Test App";
CString cwd="C://windows";

unsigned short appNameLPCW[512]; // alloc 1Kb output buffer!
unsigned short cwdLPCW[512];

MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, appName, -1, appNameLPCW, MAX_SIZE);

MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cwd, -1, cwdLPCW, MAX_SIZE);

To specify '-1' is okay as long as your input string is zero-terminated.

ralf



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top