I am trying to read a logfile in two different applications at the same time. But CreateFile (http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx) with share-mode #FILE_SHARE_READ always fails on the second call with error-code 32: ERROR_SHARING_VIOLATION: - The process cannot access the file because it is being used by another process.
When the logfile is written in modes FILE_SHARE_WRITE or FILE_SHARE_READ | FILE_SHARE_WRITE the file can not be read by any of the two applications only when FILE_SHARE_READ is used for the writer-application and then it only works if the reader-applications use FILE_SHARE_WRITE or FILE_SHARE_READ | #FILE_SHARE_WRITE, not when using #FILE_SHARE_READ. Strange .... Any suggestions?
Thank you very much.
Amendment:
Writer-Application: CreateFile(file,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0)
Reader-Applications: CreateFile(file,GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0)
Amendment 2:
When using the calls from the first Amendment the 1st Reader-Application can successfully get the filehandle, but the 2nd Reader-Application will fail with ERROR_SHARING_VIOLATION.
Amendment 3: One pointed me to the solution!
The problem was that I used logical Or instead of bitwise Or for the share-mode: FILE_SHARE_READ Or FILE_SHARE_WRITE = 1 Or 2 = 1, but it should be FILE_SHARE_READ | FILE_SHARE_WRITE = 1 | 2 = 3. So could not work as the Writer-Application required FILE_SHARE_WRITE.
FILE_SHARE_READbut somebody has the file open forGENERIC_WRITEthen the open will fail because you didn't specifyFILE_SHARE_WRITE. - Raymond Chen