Reputation: 23118
I am trying to call a function that accepts an LPTSTR as a parameter. I am calling it with a string literal, as in foo("bar");
I get an error that I "cannot convert parameter 1 from 'const char [3]' to 'LPTSTR'", but I have no idea why or how to fix it. Any help would be great.
Upvotes: 5
Views: 10747
Reputation: 7596
An LPTSTR is a non-const pointer to a TCHAR. A TCHAR, in turn, is defined as char in ANSI builds and wchar_t in Unicode builds (with the UNICODE and/or _UNICODE symbols defined).
So, an LPTSTR is equivalent to:
TCHAR foo[] = _T("bar");
As it's not const, you can't safely call it with a literal -- literals can be allocated in read-only memory segments, and LPTSTR is a signal that the callee may alter the contents of the string, e.g.
void truncate(LPTSTR s)
{
if (_tcslen(s) > 4)
s[3] = _T('\0');
}
That would crash if you passed in a literal, when compiled with Visual C++ 2008.
Upvotes: 2
Reputation: 2311
foo(const_cast<LPTSTR>("bar"));
Will crash as explained above when foo tries to change the data that's been passed to it.
Upvotes: 1
Reputation: 129944
You probably has UNICODE defined, and LPTSTR expands into wchar_t*. Use TEXT macro for string literals to avoid problems with that, e.g. foo(TEXT("bar"))
.
Upvotes: 8