Hello Brian,
I have a process on Windows which tries to open an existing file:
hFile=CreateFile(fname, // lpFileName
FILE_APPEND_DATA | // dwDesiredAccess
FILE_READ_DATA | // needed to allow LockFileEx (FILE_APPEND_DATA only results in Access denied)
DELETE,
FILE_SHARE_READ | // dwShareMode
FILE_SHARE_WRITE |
FILE_SHARE_DELETE,
&sa, // lpSecurityAttributes
OPEN_ALWAYS, // dwCreationDisposition
FILE_ATTRIBUTE_NORMAL, // dwFlagsAndAttributes
nullptr); // hTemplateFile
As you can see, I require ‘DELETE’ access, as I might need to rename (or delete) the file.
If the file exists and is viewed with FC (F3), CreateFile() fails with "The process cannot access the file because it is being used by another process”. I used sysinternals/procmon to look how FC opened the file. Well, I saw multiple calls to CreateFile() from FC but one showed the potential problem:
18:05:51,1577562 fcw.exe 2820 CreateFile C:\ccts\log\giError.log SUCCESS Desired Access: Generic Read, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: n/a, ShareMode: Read, Write, AllocationSize: n/a, OpenResult: Opened
The FILE_SHARE_DELETE flag is missing which prevents my open request with DELETE flag in dwDesiredAccess.
Attached is the log of procmon when I just press F3 to view the file, the file is opened several times, line 18 shows the one with the missing dwShareMode flag.
best regards
Torsten