How to convert char * to LPCWSTR???

Started by
5 comments, last by johnnyBravo 20 years ago
Hi i want to convert char * to LPCWSTR, how would i go about this? thanks,
Advertisement
I''m not entirely sure, but what about using TCHAR in place of char* perhaps? I can be entirely wrong, and I apologize if so.
Well, R2D22U2..
MultiByteToWideChar, probably using CP_ACP. This is during runtime.

If you just want to have a constant string use the L-Makro.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

what goes in variable ''int cchWideChar'' from the multibyte function, thanks
could you please give me an example of using multibyte thanks, like when i use the same as another one on the net it gets an access violation, i don''t know what to do, ive filled out everything properly

string song="track3.mp3";LPWSTR mp3File;MultiByteToWideChar(CP_ACP,0,song.cstr(),-1,mp3File,MAX_PATH);


so why would it be getting an access violation and crashing??

thanks,
There are some microsoft macros which make this very easy:

USES_CONVERSION; - put this in to enable the conversions

A2W(lpa) - converts char* to wchar*
W2A(lpw) - the wchar* to char*

here''s all of them (taken from MSDN):

A2CW (LPCSTR) -> (LPCWSTR)
A2W (LPCSTR) -> (LPWSTR)
W2CA (LPCWSTR) -> (LPCSTR)
W2A (LPCWSTR) -> (LPSTR)


these are all contained in AFXPRIV.H - which is MFC I think : (

but also come with ATL. If you don''t want to use either then just find the macro definitions and copy them.

Don''t do

LPWSTR mp3File;

That''s only a pointer to a wide char string. Do a

WCHAR mp3File[260];

You gave MultiByteToWideChar only a pointer to some random address. Upon writing there it would crash of course.

cchWideChar gets the size (number of w-characters) of the target buffer, in the upper case 260.

So make it

string song="track3.mp3";WCHAR  mp3File[260];MultiByteToWideChar( CP_ACP, 0, song.cstr(), -1, mp3File, 260 );

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement