All,
I'm getting seriously confused with CreateFile:
Suppose that Process-A opens a file as follows:
hFile1 = CreateFile(filename, GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
So the process has opened a file for read/write, and has told the OS that it
grants subsequent read-only access to the file. So I would have assumed that
the following would work from Process-B when opening the same file:
hFile2 = CreateFile(filename, GENERIC_READ,
FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
But it fails with ERROR_SHARING_VIOLATION. This is "explained" in the table
in the following MSDN page:
http://msdn2.microsoft.com/en-us/library/aa363874.aspx
OK fine, I can see from the table that it shouldn't work, but why not?
Process A has been quite clear that subsequent accesses on the file it
opened should succeed if the 2nd access is read-only. And if Process B then
requests read-only access why doesn't it work?
Bizarrely, if Process-B specifies "share-read, share-write" along with
"readonly" then it succeeds: