this is a followup on 'LPCTSTR typecast for use with GetFileAttributes'
i extended my method by GetLastError();
.
I cast DWORD into a unsigned int and try to display it with a MessageBox.
the code does work, but i fail to visualize the value of DWORD dw
or Unsigned int test
from the GetLastError();
method using a MessageBox and figuring how to proceed.
this a win32 project and i trying to build a method to see if a file exist on the harddrive.
there are over 15.000 error codes and it´s imposible to hardcode them all. http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx
BOOL FileExists(LPCTSTR szPath)
{
//MessageBox(NULL,szPath,L"File Error",MB_OK);
DWORD dwAttrib = GetFileAttributes(szPath);
unsigned int test;
DWORD dw = GetLastError();
test =(unsigned int)dw;
if(test == 0){
MessageBox(NULL,(LPCWSTR)test, L"Target == 0",MB_OK);
}
else
{
MessageBox(NULL,(LPCWSTR)test, L"Target != 0",MB_OK);
}
switch(dw)
{
case ERROR_SUCCESS:
MessageBox(NULL,L"ERROR_SUCCESS", L"File Error",MB_OK);
break;
case ERROR_PATH_NOT_FOUND:
MessageBox(NULL,L"ERROR_PATH_NOT_FOUND", L"File Error",MB_OK);
break;
default:
MessageBox(NULL,(LPCWSTR)dw, L"File Error",MB_OK);
break;
}
switch(dwAttrib)
{
case FILE_ATTRIBUTE_DIRECTORY:
MessageBox(NULL,L"FILE_ATTRIBUTE_DIRECTORY", L"File Error",MB_OK);
break;
case FILE_ATTRIBUTE_ARCHIVE:
MessageBox(NULL,L"FILE_ATTRIBUTE_ARCHIVE", L"File Error",MB_OK);
break;
case FILE_READ_ONLY_VOLUME:
MessageBox(NULL,L"FILE_READ_ONLY_VOLUME", L"File Error",MB_OK);
break;
case FILE_INVALID_FILE_ID:
MessageBox(NULL,L"FILE_INVALID_FILE_ID", L"File Error",MB_OK);
break;
//case INVALID_FILE_ATTRIBUTES:
// MessageBox(NULL,L"INVALID_FILE_ATTRIBUTES",L"File Error",MB_OK);
// break;
//case FILE_INVALID_FILE_ID:
// MessageBox(NULL,L"Failed to get image file,\\please check game folder",L"File Error",MB_OK);
// break;
default:
MessageBox(NULL,(LPCWSTR)dwAttrib,L"File Error",MB_OK);
break;
}
return true; // testing phase
}
You cannot type-cast a
DWORD
into a string. You have to format it.Your error checking is wrong.
On success,
GetFileAttributes()
can (and frequently does) return multiple attributes at a time, but you are not handling that possibility. And some of the values you are checking for are not even value attributes to begin with.Try this instead: