I'm trying to convert string to 'LPCTSTR', but, i got following error.
Error :
cannot convert from 'const char *' to 'LPCTSTR'
code:
std::string str = "helloworld";
LPCTSTR lp = str.c_str();
Also, tried :
LPCTSTR lp = (LPCTSTR)str.c_str();
But, print garbage value.
LPCTSTR
means (long pointer to constantTCHAR
string).A
TCHAR
can either bewchar_t
orchar
based on what your project settings are.If, in your project settings, in the "General" tab, your character set is "Use Multi-byte character set" then
TCHAR
is an alias forchar
. However, if it's set to "Use Unicode character set" thenTCHAR
is an alias forwchar_t
instead.You must be using the Unicode character set, so:
Is in reality:
This is why you're getting the error:
Your line:
Is in reality:
In a
std::string
, the chars are single bytes, having awchar_t*
point to them will expect that each character is 2+ bytes instead. That's why you're getting nonsense values.The best thing to do would be as Hans Passant suggested - not to use typedefs based on
TCHAR
. In your case, do this instead:If you want to use wide chars, which Windows calls Unicode, then you can do this: