Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CreateFile GetFileTIme SetFileTime

Tags:

c++

winapi

I'm having trouble using GetFileTime and SetFileTime when it comes to directories. Specifically I think my problem is that I am new to the WinAPI and I don't think I'm getting the HANDLE correctly.

There are 2 scenarios.

In the first, I just need a handle to get the file or directory timestamps (create,access,mod). I'd like to make this handle in a safe and flexible way. Don't want to be too generous in the parameters.

In the second, I need a handle that will allow me to modify the file or direcotry timestamps. I'd also like to create this handle with the minimum rights but in flexible and dependable way.

By flexible I mean that in both scenarios I need the code to work both localy, in a network share, and in a multi-threaded app. The multi-threaded part is not necessary because my app will not make multiple handles on the file/dir but it is possible that some other app running in the background will.

//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).
like image 497
user440297 Avatar asked Feb 15 '11 00:02

user440297


1 Answers

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

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

I would guess this would work for Answer #4 as well, but I haven't tried it to confirm.

like image 52
Joel Rondeau Avatar answered Sep 25 '22 14:09

Joel Rondeau