Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Language: C

Platform: Windows 10

Compiler: MinGW

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
    }

    LPWSTR path = malloc(wcslen(*(dir + 1))*sizeof(wchar_t) + 2);
    wcscpy(path, *(dir + 1));
    DWORD fileAttribute = GetFileAttributesW(path);
    wprintf(L"fileAttribute: %d\n", fileAttribute);
    free(path);
#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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading