LPCTSTR
is a pointer to an array of const wchar_t
. strPermission
points to the first character of the array. L"0"
is a string literal, which is an array of const wchar_t
, which decays to a pointer of const wchar_t
. But the pointers are not equal, they point to different arrays. This is why we invented C++. Please use it.
std::wstring strPermission = Method();
return (strPermission == L"0"); //works like magic!
or, if Method
is returning something you have to retain, at least do this
std::unique_ptr<wchar_t[]> strPermission = Method();
return (std::wcscmp(strPermission.get(), L"0")==0);
//wcscmp is for comparing strings, but returns 0 if they're equal.
Also, are you sure that strPemission
points to an array that contains a zero character followed by a null character? If not, and you're not using wstring
, then you also have to check that it points at an array
if (strPermission)
//do stuff
else
//its a NULL pointer.
I have been prodded by chris to point out that the type of LPCTSTR
actually depends on the compiler options. I can tell by your code that you're coding with _UNICODE
set, which makes it a const wchar_t*
, but if you want to be able to compile with other options (I can't think of a good reason to do so) you should use _tcscmp
to compare, have the literals as _T("0")
and they'll be arrays of TCAHR
. For the strings, you'll have to add a typedef somewhere:
#ifdef _UNICODE
typedef std::string std::tstring
//you'll probably have to add more t helper functions here
#else
typedef std::string std::wstring
//you'll probably have to add more t helper functions here
#endif
If you want to be certain that your code is always _UNICODE
(which is what I do), explicitly call MethodW()
instead of Method()
. (There is also a corresponding MethodA()
, but not much reason to call it).
There's also a UNICODE
macro, but it should always be the same as the _UNICODE
macro. (Never define these yourself, they belong in the project options)