Landin Martens
Landin Martens

Reputation: 3333

C++ MFC how to compare LPCTSTR in a if statement?

I have the following code:

LPCTSTR strPermission = Method();

if (strPermission == L"0")
{
    return true;
}
else
{
    return false;
}

While debugging I can see that strPermission does equal "0", yet when I compare it like in the if statement it always returns false.

The only thing I can think of is that it is comparing the memory address of the variable rather than the variable value.

How do I compare strPermission to L"0" so that it would return true if strPermission equals "0".

Thank you!

Upvotes: 4

Views: 10106

Answers (3)

Mooing Duck
Mooing Duck

Reputation: 66961

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)

Upvotes: 1

AVIDeveloper
AVIDeveloper

Reputation: 3516

You'll need to use a C runtime library function. strcmp compares ANSI strings, wcscmp compares UNICODE strings.

You use it like this:

bool match = wcscmp(strPermission, L"0") == 0;

Upvotes: 4

Carl Norum
Carl Norum

Reputation: 225132

You can't compare C-style strings like that in C or C++. Check out this C FAQ question & answer.

The function you're looking for is called lstrcmp.

Upvotes: 2

Related Questions