Click to See Complete Forum and Search --> : File Sharing Violation?????????
yiannakop
March 11th, 2004, 08:32 AM
Hi. I am writing a program that at some point updates the data of a file (it doesn't matter, but the file is a .wav file). The program seems to work fine (no memory leaks etc).
If the file is choosen in a windows explorer window ( I have Win 2k), on the left of the window we can see a preview of the file and some descriptions of it.
The problem is that if the file is kept chosen and I run the program again, the program crashes (something about fseek: sharing violation). Why is this happening? Isn't the windows explorer supposed to open the file for read access ONLY? If yes this shouldn't have happened.
Thanx
yiannakop
March 12th, 2004, 10:05 AM
Any ideas??? Please help
dimm_coder
March 12th, 2004, 11:08 AM
That seems the explorer application opens a file setting a blocking lock (to prevent writting by other applications). In any casy, your application should carefully process that case.
Where your application gets an error? what system generates it?
yiannakop
March 12th, 2004, 11:59 AM
Originally posted by dimm_coder
Where your application gets an error? what system generates it?
The application crashes in fseek. Probably fopen returns NULL for the reasons you said.
dimm_coder
March 12th, 2004, 12:40 PM
Originally posted by yiannakop
The application crashes in fseek. Probably fopen returns NULL for the reasons you said.
Yes. Check it.
In any case, U should do it to prevent any unspecified behavior of your program.
yiannakop
March 15th, 2004, 08:12 AM
Thanx. I know I have to check in my program if pointer is NULL. I just wanted to know WHY windows explorer BLOCKS the file from further writting...
dimm_coder
March 15th, 2004, 09:08 AM
Originally posted by yiannakop
Thanx. I know I have to check in my program if pointer is NULL. I just wanted to know WHY windows explorer BLOCKS the file from further writting...
Well, if it shows some info concerning to the given file, it wants to be sure that noone is able to update this file while it's showing it. Ussualy in theory of synchronization: 1) some readers can simultaneously have access to the specified object (file, etc...) for reading and at the same time noone is able to access it for writting. It will be chaos, if someone's changing the object and another one's reading it that moment. 2) Or only one writer can have access to the object for writting and at the same time noone is able to read it, while it's blocked for writting.
So, here we have the case 1).
According to your words, it seems that explorer keeps an opened handle to the file, while showing info about it or smth.
yiannakop
March 16th, 2004, 08:50 AM
Thanx. I understand what you are saying. Though, in C for example, you can have one handle for writting to a file and at the same time another handle for reading from the same file.
dimm_coder
March 16th, 2004, 09:05 AM
It depends on the library implementation. C run-time library calls (fopen(), etc...) seem not to set any kind of locks for file. But if someone directly calls the CreateFile() system call and set the locking for writting, noone is able to open it.
Well, U can read in MSDN:
Read here about the "dwShareMode" param:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/createfile.asp
And here about some ways to open a file in different modes:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/creating_and_opening_files.asp
yiannakop
March 16th, 2004, 09:57 AM
You're right. fopen does not lock the file. I thought CreateFile works in the same way, but it doesn't.
Thanx
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.