Comparing two TCHAR's with same value results false
Asked Answered
L

2

5

I am trying to check my applications path, and if it is not a specified path, then move it. I feel like my code is solid, yet it does not work properly.

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 when the file is in the new path it will still result pCheck != xPath

I never get the "New Location" messagebox. When first two messageboxs are displayed, the path's are equal...

Larch answered 10/11, 2012 at 16:46 Comment(0)
S
12

You are just comparing the addresses of the arrays (which obviously are never the same). If you want to compare the two strings held in the arrays you should use _tcscmp();

if(_tcscmp(pCheck, xPath) != 0){ 
Steinman answered 10/11, 2012 at 16:48 Comment(2)
Thank you. I will select as answer in 10 mins when it lets me.Larch
Note: _tcsicmp for case-insensitive comparisons.Omentum
R
3

Your TChar array decays into a pointer to the first character. You are currently checking for pointer equality, which is why you never get TRUE.

Use strcmp equivalents, for TChar, like _tcscmp.

_tcscmp is a macro, which either invokes wcscmp or strcmp depending on the type of characters.

Rollick answered 10/11, 2012 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.