Sign in to follow this  
spacejim

converting wchar_t to LPTSTR?

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this