Home > Enterprise >  C and Win32: Getting non-documented return value from GetFileAttributesW() function
C and Win32: Getting non-documented return value from GetFileAttributesW() function

Time:07-23

Language: C

Platform: Windows 10

Compiler: MinGW

I am obtaining a return value from the GetFileAttributesW() function (part of the Win32 API) equal to 17, which does not match any of the file attribute macros listed here, nor is it equal to the macro INVALID_FILE_ATTRIBUTES (-1).

Upon calling GetLastError() after the function call, ERROR_SUCCESS is returned (0) - indicating no error. So I am perplexed as to what is going on.

The file in question (passed as a command-line argument) is my Downloads directory, which is why I find it odd that the value returned by GetFileAttributesW() (17) is only one number off of FILE_ATTRIBUTE_DIRECTORY (16).

Minimum reproducible example:

#include <stdio.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <sys/stat.h>
#include <time.h>
#include <dirent.h>
#endif

int main(int argc, char **argv) {

    if (argc != 2) {
        fprintf(stderr, "Invalid number of arguments provided.\n");
        return -1;
    }

#ifdef _WIN32
    int Argc;
    LPWSTR *dir = CommandLineToArgvW(GetCommandLineW(), &Argc);

    if (dir == NULL) {
        fprintf(stderr, "Error getting command line arguments.\n");
        return -1;
    }
    DWORD fileAttribute = GetFileAttributesW(*(dir   1);
    wprintf(L"fileAttribute: %d\n", fileAttribute);
#endif
    
    return 0;
}

There might be something simple that I'm missing, or perhaps there is no clear solution. Any help would be appreciated, thanks.

CodePudding user response:

The return value is a bitmask, multiple flags can be combined using the bitwise OR operator.

The value 17 is FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_READONLY.

You can use the bitwise AND operator to check if the value contains each flag.

  • Related