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

Landin Martens picture Landin Martens · Mar 21, 2012 · Viewed 8.6k times · Source

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!

Answer

AVIDeveloper picture AVIDeveloper · Mar 21, 2012

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;