Reputation: 625
New to windows programming, there are several examples all over the internet of what i am about to ask however none of them show the comparison which i think is failing.
I'm using several windows api calls throughout my C++ Program and just need some guidence on how to use them correctly.
For example below i have GetFileAttributes() which returns anything from File Attribute Constants.
DWORD dwAttributes = GetFileAttributes(strPathOfFile.c_str());
if ( dwAttributes != 0xffffffff )
{
if ( dwAttributes == FILE_ATTRIBUTE_NORMAL )
{
pkFileInfoList->Add( strPathOfFile + "\t" +"FILE_ATTRIBUTE_NORMAL");
}
else if ( dwAttributes == FILE_ATTRIBUTE_ARCHIVE )
{
pkFileInfoList->Add( strPathOfFile + "\t" + "FILE_ATTRIBUTE_ARCHIVE");
}
}
[/CODE]
The if/else statement continues with everything from File Attribute Constants.
Am i using this correctly, i have a directory with over 2500 files which i am feeding the path recusivly. It is always returning FILE_ATTRIBUTE_ARCHIVE.
Thanks,
Upvotes: 0
Views: 662
Reputation: 2807
GetFileAttributes
returns a set of attributes, not a single attribute, so to test correctly you should do:
DWORD dwAttributes = GetFileAttributes(strPathOfFile.c_str());
if ( dwAttributes != 0xffffffff )
{
if ( dwAttributes & FILE_ATTRIBUTE_NORMAL )
{
pkFileInfoList->Add( strPathOfFile + "\t" +"FILE_ATTRIBUTE_NORMAL");
}
else if ( dwAttributes & FILE_ATTRIBUTE_ARCHIVE )
{
pkFileInfoList->Add( strPathOfFile + "\t" + "FILE_ATTRIBUTE_ARCHIVE");
}
}
I.e. use bitwise & instead of ==
Upvotes: 3