GetFileAttributes() keeps returning -1

Hi all, I have a function in my win32 program that checks if a file is read only, but no matter what i do GetFileAttributes returns an error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool FindAndDeleteFile(HWND hWnd)
{
	WIN32_FIND_DATA first;
	HANDLE hFirst;
	WCHAR* FirstFileName;

	hFirst = FindFirstFile(L"C:\\a", &first);
        FirstFileName = first.cFileName;

	DWORD Attributes;
	Attributes = GetFileAttributes(first.cFileName);

	if(Attributes & INVALID_FILE_ATTRIBUTES)
	{
		MessageBoxA(hWnd, "Returned -1", "Error", MB_OK);
	}

        return TRUE;
}


My message box keeps popping up, hence GetFileAttributes returns INVALID_FILE_ATTRIBUTES.

and I merely call my function on a button press in WM_COMMAND so i know it's not some other piece of code giving me problems.

Anyone know what i'm doing wrong?
Last edited on
Bumpity bump.
i need help
Have you tried calling GetLastError() to see what caused the INVALID_FILE_ATTRIBUTES return?
I'll give it a shot, but how do i display the return value for that function(MessageBox() or what)
You have to use FormatMessage() there's an example near the bottom of the page here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms679351(v=vs.85).aspx
You are checking the return value wrong. In case of failure MSDN page says it returns INVALID_FILE_ATTRIBUTES.

So the correct code will be this:
1
2
3
4
5
6
7
8
9
DWORD Attributes;
	Attributes = GetFileAttributes(first.cFileName);

	if(INVALID_FILE_ATTRIBUTES == Attributes)
	{
		MessageBoxA(hWnd, "Returned -1", "Error", MB_OK);
	} else {
                MessageBoxA(hWnd, "Returned success", "Error", MB_OK);
        }


http://msdn.microsoft.com/en-us/library/windows/desktop/aa364944%28v=vs.85%29.aspx
Last edited on
Topic archived. No new replies allowed.