CreateFile GetFileTIme SetFileTime

I'm having trouble using GetFileTime and SetFileTime when it comes to directories. In particular, I believe that my problem is that I am new to WinAPI, and I do not think that I am getting the Pen correctly.

There are 2 scenarios.

In the first, I just need a handle to get the file or directory timestamps (create, access, mod). I would like to make this handle safe and flexible. Do not want to be too generous in options.

In the second, I need a handle that allows me to change the file or timestamp directive. I would also like to create this descriptor with minimal rights, but in a flexible and reliable way.

Flexible, I mean, in both scenarios I need code to work both locally and in a network resource, as well as in a multi-threaded application. The multi-threaded part is not needed because my application will not create multiple / dir file descriptors, but it is possible that some other application is running in the background.

//QUESTION 1:
//I do this when I just need a handle to **GET** some attributes like dates.
//(here I just need a handle to get info I am not modding the item).
//Am I using the correct params if I need it to work in a 
//local + networked environment and also in a multi-threaded app???
h1 = CreateFile(itemA, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
if (h1 == INVALID_HANDLE_VALUE){

    return 0;
}
//QUESTION 2:
//The above works for local files but not local dirs.
//How can I get the above to work for dirs? (Same environment considerations).


//QUESTION 3:
//I do this when I just need a handle to ***SET*** some attributes (like timestamps).
//(here I need a handle that allows me to modd the items timestamp).
//Am I using the correct params if I need it to work in a 
//local + networked environment and also in a multi-threaded app???
hItemB = CreateFile(itemB, FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
if (hItemB == INVALID_HANDLE_VALUE){
    return 0;
}
//QUESTION 4:
//The above works for local files but not local dirs.
//How can I get the above to work for dirs? (Same environment considerations).
+3
source share
2 answers

Answer # 2: To use CreateFile, to get a directory descriptor, you need to use a flag FILE_FLAG_BACKUP_SEMANTICS. Using your example:

h1 = CreateFile(itemA, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);

I would suggest that this would work for answer # 4 as well, but I have not tried to confirm it.

+4
source

Here is a sample code on how to set a directory date based on a DOS timestamp.

int Directory_SetDosTime(char *Path, unsigned int DosDateTime)
{
    FILETIME LocalTime, FileTime;
    HANDLE Handle;
    SYSTEMTIME SystemTime;


    DosDateTimeToFileTime((DosDateTime >> 16), DosDateTime, &LocalTime);
    LocalFileTimeToFileTime(&LocalTime, &FileTime);
    FileTimeToSystemTime(&FileTime, &SystemTime);

    Handle = CreateFile(Path, GENERIC_WRITE, FILE_SHARE_WRITE,
                    NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);

    if (Handle == INVALID_HANDLE_VALUE)
    {
        //Unable to open directory
        return FALSE;
    }

    if (SetFileTime(Handle, &FileTime, &FileTime, &FileTime) == 0)
    {
        //Unable to set directory time
        CloseHandle(Handle);
        return FALSE;
    }

    CloseHandle(Handle);
    return TRUE;
}
0
source

Source: https://habr.com/ru/post/1792122/


All Articles