Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can CreateFile fail with FILE_SHARE_READ and succeed with FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE?

Try it yourself:

Create an XLS file, open it in Excel.

Open sysinternals Process Monitor, and watch what happens while you make a copy of your XLS file in the explorer (just hit ctrl-c ctrl-v).

Two calls to ::CreateProcess in a row. First call asks for read permissions, and gets Access denied. Second call asks for read plus write plus delete and passes.

Is that normal?

like image 859
Benoit Avatar asked Mar 17 '11 13:03

Benoit


1 Answers

If you open a file with FILE_SHARE_READ you're saying you're willing to share access to this file, but only for reads.

If you open with all the flags, you're willing to share access also for writes/delete.

FILE_SHARE_READ is more restrictive than FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE

If some other process (excel) has this file opened for e.g. write (and it has the sharing flags set), the only way you can access it is to accept sharing it for write.

like image 111
Erik Avatar answered Sep 21 '22 10:09

Erik