user7967742
user7967742

Reputation:

Set File attribute succed but retriving it fails

I use SetFileAttributesW to set custome file attribute e.g. 0x200008, and the SetFileAttributesW returns nonzero means that no error. but GetFileAttributesW retrieves file attribute ignoring the set attribute.

int main()
{
    uint32_t magic = 0x200008;
    DWORD attribute = GetFileAttributesW(L"test");
    cout << attribute << endl;
    if ((attribute & magic) == magic)
        cout << "has magic" << endl;
    else
    {
        attribute |= magic;
        cout << attribute << endl;
    }
    cout << SetFileAttributesW(L"test", attribute) << " " << GetLastError();
    cin.get();
    return 0;
}

and the output for each run
32
2097192
1 0

Any one can help me?

Best.

Upvotes: 0

Views: 488

Answers (2)

Adrian Bura
Adrian Bura

Reputation: 56

It seems you use an value that is not supported. 0x200008 is not documented by Microsoft.

See for supported constants: [https://msdn.microsoft.com/en-us/library/windows/desktop/gg258117(v=vs.85).aspx][1]

Upvotes: 0

RbMm
RbMm

Reputation: 33706

not any attributes valid, if you look in wmd.h you can find next definition:

#if (_WIN32_WINNT < _WIN32_WINNT_WIN8)

#define FILE_ATTRIBUTE_VALID_FLAGS          0x00007fb7
#define FILE_ATTRIBUTE_VALID_SET_FLAGS      0x000031a7

#else

#define FILE_ATTRIBUTE_VALID_FLAGS          0x0002ffb7
#define FILE_ATTRIBUTE_VALID_SET_FLAGS      0x000231a7

#endif

so your custom file attribute 0x200008 not valid and will be undefined behavior. however in current implementation file systems ignore any values that they don't understand instead return error See the FASTFAT source:

    //
    //  Only permit the attributes that FAT understands.  The rest are silently
    //  dropped on the floor.
    //



    Attributes = (UCHAR)(Buffer->FileAttributes & (FILE_ATTRIBUTE_READONLY |
                                                   FILE_ATTRIBUTE_HIDDEN |
                                                   FILE_ATTRIBUTE_SYSTEM |
                                                   FILE_ATTRIBUTE_DIRECTORY |
                                                   FILE_ATTRIBUTE_ARCHIVE));

NTFS do the same.

so use only attributes which declared in windows header files and in mask FILE_ATTRIBUTE_VALID_SET_FLAGS

Upvotes: 1

Related Questions