converting wchar_t to LPTSTR?

Started by
4 comments, last by spacejim 17 years, 6 months ago
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"?
------------------physics-editor.com
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
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.
Why not use _tcscpy()?
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 );
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*.
------------------physics-editor.com

This topic is closed to new replies.

Advertisement