Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare TCHAR with String value in VC++?

Tags:

visual-c++

How to Compare TCHAR with String value in VC++ ? My project is not Unicode. I am doing like this :

TCHAR achValue[16523] = NULL;
if(achValue == _T("NDSPATH"))
            {
                return FALSE;
            }

When achValue = "NDSPATH" then also this condition does not staisfy.

Any help is appreciated.

like image 555
Swapnil Gupta Avatar asked Sep 01 '25 04:09

Swapnil Gupta


1 Answers

A TCHAR or any string array is simply a pointer to the first character. What you're comparing is the value of the pointer, not the string. Also, you're assigning an array to null which is nonsensical.

Use win32 variations of strcmp. If you use the _tcscmp macro, it will use the correct function for multibyte/unicode at compile time.

#define MAX_STRING 16523;

TCHAR achValue[MAX_STRING];
ZeroMemory(achValue, sizeof(TCHAR) * MAX_STRING);

sprintf(achValue, MAX_PATH, _T("NDSPATH"));

if (!_tcscmp(achValue, _T("NDSPATH"))
{
    // strings are equal when result is 0
}
like image 196
my fat llama Avatar answered Sep 06 '25 18:09

my fat llama