Archived

This topic is now archived and is closed to further replies.

johnnyBravo

How to convert char * to LPCWSTR???

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites