Comparing two TCHARs with identical false values

I am trying to check my path to applications, and if this is not a given path, move it. I feel that my code is solid, but it does not work correctly.

TCHAR pCheck[MAX_PATH]; TCHAR xPath[MAX_PATH]; GetModuleFileName(NULL,xPath,MAX_PATH); if(SHGetSpecialFolderPath(HWND_DESKTOP, pCheck, CSIDL_DESKTOP, FALSE)){ wcscat(pCheck,L"\\NewFile.exe"); MessageBox(NULL,pCheck,NULL,NULL); MessageBox(NULL,xPath,NULL,NULL); if(pCheck!=xPath){ CopyFile(xPath,pCheck,0); ShellExecute(0,L"open",pCheck,NULL,NULL,SW_SHOW); return 0; }else{ MessageBox(NULL,L"New Location",NULL,NULL); return 0; } } 

Even if the file is in a new path, it will still have the value pCheck! = XPath

I never get the "New Location" message. When the first two message boxes are displayed, the path is ...

+4
source share
2 answers

You just compare the addresses of the arrays (which obviously never match). If you want to compare two strings stored in arrays, you should use _tcscmp();

 if(_tcscmp(pCheck, xPath) != 0){ 
+6
source

Your TChar array breaks into a pointer to the first character. You are currently checking for pointer equality, so you never get TRUE .

Use strcmp equivalents for TChar, for example _tcscmp .

_tcscmp - macro , which either calls wcscmp or strcmp depending on the type of characters.

+2
source

Source: https://habr.com/ru/post/1445227/


All Articles