0

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,

1 Answer 1

3

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 ==

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.