Re: shareDenyNone

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/06/04


Date: Thu, 06 May 2004 14:51:25 -0400

Once you do this, it becomes your responsibility to ensure that simultaneous writers
cannot ever corrupt the data. This means you have to do explicit file locking yourself
(LockFileEx, UnlockFileEx) on portions of the file. In addition, the following code is
always guaranteed to be unreliable:

LONGLONG n = f.GetFileSize();
if(n < SOME_MAX_CONSTANT)
    ... go ahead and do it

because between the time GetFileSize was done and n was found to be some known value and
the time you tested it, some other thread got scheduled, ran at high priority, and added a
megabyte of data to the file.

The correctness of data is not guaranteed if you are reading data while another thread is
writing in the same area. You could get any mix of old and new data. That's why you have
to do LockFileEx. Even when extending (note that you can lock areas beyond the
end-of-file, so that you are guaranteed you are the only one appending). And if two
threads are writing, all bets are completely off. You are likely to end up with garbage.
                                        joe

On Thu, 6 May 2004 10:00:05 -0600, "Jonathan Wood" <jwood@softcircuits.com> wrote:

>Hello,
>
>I have had some customers request that my software be able to open files
>that are in use by other applications. Previously, I was opening files with
>the shareDenyWrite attribute. If I change this to shareDenyNone, then I am
>able to open the files in question.
>
>Does anyone have any experience with shareDenyNone? I'm concerned I would
>start to read the file and problems could arise if another process was
>writing to it at the same time. Currently, my application also checks that
>the file is not too big, but that could change if another process was
>writing to the file as I read it.
>
>Any thoughts?
>
>Thanks.

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm



Relevant Pages

  • shareDenyNone
    ... I have had some customers request that my software be able to open files ... I was opening files with ... Does anyone have any experience with shareDenyNone? ... writing to the file as I read it. ...
    (microsoft.public.vc.mfc)
  • Re: WriteFile
    ... How do you know it is writing "incomplete data"? ... >I am using WriteFile function for writting some data to harddisk. ... >Basically this function is writting imcomplete data. ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: Steven Goldmans picks for regular season awards
    ... GaryFL wrote: ... I am very surprised at his AL MVP vote and his AL MOY vote. ... believe his writing might be. ... You are in the 10% of fans who get what he's ...
    (alt.sports.baseball.ny-yankees)
  • Re: How to "zero" a file after a certain size ?
    ... Michael Harris (MVP) wrote: ... >>Deleting the file would not work because some process is still ... > Use FSO to open it ForWriting and close it without writing anything... ... -- torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway Administration scripting examples and an ONLINE version of the 1328 page Scripting Guide: ...
    (microsoft.public.windows.server.scripting)
  • Re: Too Much Code
    ... Greg Young ... MVP - C# ... I am writing a large and complex sub procedure in a form, ...
    (microsoft.public.dotnet.general)