Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 331
Landin Martens Avatar asked Oct 29 '25 13:10

Landin Martens


2 Answers

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;
like image 124
AVIDeveloper Avatar answered Oct 31 '25 03:10

AVIDeveloper


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.

like image 36
Carl Norum Avatar answered Oct 31 '25 04:10

Carl Norum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!