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.
LPCTSTRmeans (long pointer to constantTCHARstring).A
TCHARcan either bewchar_torcharbased 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
TCHARis an alias forchar. However, if it's set to "Use Unicode character set" thenTCHARis an alias forwchar_tinstead.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: