• Advertisement
Sign in to follow this  

converting wchar_t to LPTSTR?

This topic is 4152 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is a stupid question I know, because LPTSTR should boil down to a wchar_t*. I have:
LTPSTR msg = new TCHAR[10];
wchar_t* w = "moo";

wcscpy((wchar_t*)msg, w);
but the result: msg == "m" if I do this afterwards wcslen((wchar_t*)msg); it returns 3, so what is going wrong that the system is only seeing the first char "m"?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by spacejim
This is a stupid question I know, because LPTSTR should boil down to a wchar_t*.

Not neccessarily. If you don't have your program set up to use UNICODE, I believe LPTSTR is just a regular old char*. I can't really offer any advice as to your specific problem, except to suggest verifying your project is set up properly.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by spacejim
This is a stupid question I know, because LPTSTR should boil down to a wchar_t*.

I have:

wchar_t* w = "moo";


The compiler should be complaining about this. In C++, literals have a specific type, which isn't influenced by the context (i.e. where they're being assigned to). "moo" is of type char*, and while an implicit conversion between pointers may be possible, it certainly won't automatically widen the pointed-at characters. A pointer is just an address, after all.

You need something like L"moo" or TEXT("moo") or some other such markup.

Share this post


Link to post
Share on other sites
check WideCharToMultiByte from msdn
maybe this is the answer that you seek.



wchar_t *wtest = L"abcdef";
char buf[1024];
WideCharToMultiByte( CP_UTF8, 0, wtest, -1, buf, 1024, 0, 0 );
printf( "%s", buf );

Share this post


Link to post
Share on other sites
Thanks, turns out I was mis-interpreting the LPTSTR I had coming in from an external source, it was char* all along, while I was assuming wchar*.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement