Discussion:
FILE_SHARE_READ and FILE_SHARE_WRITE
(too old to reply)
PEACEMAKER
21 years ago
Permalink
I have an application that needs to be able to open any file for reading.
Currently I only use FILE_SHARE_READ but I looking at the docs it mentions
that if a file was opened by another processes with FILE_SHARE_WRITE then
all subsequent open operations on the file has to include this flag or
opening will fail (eg if I only specificy FILE_SHARE_READ). The docs don't
say what happens in the reverse case.. eg a file is created with
FILE_SHARE_READ, but I attempt to open it with
FILE_SHARE_READ|FILE_SHARE_WRITE. Which flags should I use to be able to
open all shared files without any problems?
Gary Chanson
21 years ago
Permalink
...
Not quite. The file sharing flags determine what file modes are
permitted to other handles to the same file object. In other words, if I
open a file with FILE_SHARE_READ, the file can also be opened for read by
another call to open. If I specify FILE_SHARE_WRITE, the file can be opened
for write by another call. If I specify neither, the file cannot be opened
by another call. Any subsequent open has to specify whatever flags are
needed to account for my open mode. If I open the file for read, they must
specify FILE_SHARE_READ and so on.
Post by PEACEMAKER
Which flags should I use to be able to
open all shared files without any problems?
This isn't possible. The best you can do is to specify both
FILE_SHARE_READ and FILE_SHARE_WRITE. This will permit you to open any file
for reading which has specified FILE_SHARE_READ but you will still not be
able to open files opened in exclusive mode.
--
-GJC [MS Windows SDK MVP]
-Software Consultant (Embedded systems and Real Time Controls)
- http://www.mvps.org/ArcaneIncantations/consulting.htm
-***@mvps.org
hector
21 years ago
Permalink
PeaceMaker, look at it this way:

The GENERIC_XXXX flags define HOW you will access a file.

The FILE_SHARE_XXXX flags define how OTHER are allowed to access the file
while you got it open.

So if you need to open a file for reading, that means:

GENERIC_READ

If by chance the same file is already open by other program, this
GENERIC_READ will only work if the other process used FILE_SHARE_READ. If
it had FILE_SHARE_WRITE or 0, you will get an CreateFile() file share error,
I forget either 5 or 32.

So now you were successful to open it for your reading. Now what?

Do you want to stop others from reading too? Or writing to it? or both? or
no access at all (Exclusive Mode)?

This is where you would use the FILE_SHARE_XXXXX flags or 0 (Zero) for no
sharing.

This all depends on your needs.

Typically, if you are reading it only, you don't stop others from writing it
UNLESS you know for sure what file sharing logic they are using and that it
matches your file sharing logic.

If you are writing to a file, you probably only want to another program to
read it (FILE_SHARE_READ). This will stop them from writing to the file at
the same time you are.

But again it all depends on having a consistent file sharing I/O logic and
that requires understand how the other programs work too :-).
...
Loading...