4

I have a Delphi 2006 app that writes to a file and then regularly renames it to a sequenced name, and creates a new one, i.e.

open file.dat
write a record to file.dat
close file.dat 
...   
open file.dat
write a record to file.dat
close file.dat  

rename file.dat to file20110818123456.dat

create file.dat

open file.dat
write a record to file.dat
close file.dat  
...

The problem is that occasionally the rename fails with Error 32 - The process cannot access the file because it is being used by another process. I am checking that the destination of the rename doesn't exist. It's as if the close of the file after writing a record doesn't happen immediately. I added a loop in the rename procedure to sleep for a bit and try again up to 10 times but that doesn't help.

function RenameFileAtAllCosts (const OldFileID : TFilename ;
                               const NewFileID : TFilename ;
                               out   ErrorCode : Integer) : boolean ;

const
    MaxRenameAttempts  = 10 ;

var
    RenameAttempts     : integer ;

begin
Result := FileExists (OldFileID) ;

if (Result = true) then
    begin
    if FileExists (NewFileID) then
        begin
        Result := DeleteFile (PChar (NewFileID)) ;
        end ;

    if (Result = true) then
        begin
        Result := (not FileExists (NewFileID)) ;
        end ;

    if (Result = true) then
        begin
        RenameAttempts := 0 ;
        repeat
            SysUtils.Sleep (50) ;   
            Result := RenameFile (OldFileID, NewFileID) ;
            inc (RenameAttempts)
        until (Result or (RenameAttempts >= MaxRenameAttempts)) ;
        end ;
    end ;
if (not Result) then
    begin
    ErrorCode := GetLastError ;
    end ;
end ;

* ADDITIONAL INFO *

This is the ProcMon log of the relevant disk I/O, with the associated Pascal source or psuedo-code preceding each block

open file.dat
write a record to file.dat
close file.dat 

5:45:27.1718325 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Desired Access: Generic Read/Write, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: N, ShareMode: Read, Write, AllocationSize: n/a, OpenResult: Opened
5:45:27.1719739 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,208, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:27.1720475 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,208, Length: 28, Priority: Normal
5:45:27.1721403 PM  APP.exe 1276    CloseFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS

if FileExists (NewFileID) then    // before call to RenameFileAtAllCosts
    begin

5:45:27.8630005 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP20110819054527.DAT    NAME NOT FOUND  Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a

if DeleteFile (NewFileID) then    // before call to RenameFileAtAllCosts
    begin

5:45:27.8634050 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP20110819054527.DAT    NAME NOT FOUND  Desired Access: Read Attributes, Delete, Disposition: Open, Options: Non-Directory File, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a

Result := FileExists (OldFileID) ;

5:45:27.8640878 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
5:45:27.8641684 PM  APP.exe 1276    QueryBasicInformationFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS CreationTime: 2/08/2011 7:45:59 PM, LastAccessTime: 19/08/2011 4:25:33 PM, LastWriteTime: 19/08/2011 5:45:27 PM, ChangeTime: 19/08/2011 5:45:27 PM, FileAttributes: A
5:45:27.8641902 PM  APP.exe 1276    CloseFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS

    if FileExists (NewFileID) then
        begin  

5:45:27.8648698 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP20110819054527.DAT    NAME NOT FOUND  Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a


    if (Result = true) then
        begin
        Result := (not FileExists (NewFileID)) ;
        end ;

5:45:27.8656780 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP20110819054527.DAT    NAME NOT FOUND  Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a

        RenameAttempts := 0 ;
        repeat
            SysUtils.Sleep (50) ;   
            Result := RenameFile (OldFileID, NewFileID) ;
            inc (RenameAttempts)
        until (Result or (RenameAttempts >= MaxRenameAttempts)) ;

5:45:27.9211195 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SHARING VIOLATION   Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:27.9834427 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SHARING VIOLATION   Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.0459285 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SHARING VIOLATION   Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.1084086 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SHARING VIOLATION   Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.1710646 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SHARING VIOLATION   Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.2335139 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SHARING VIOLATION   Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.2959037 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SHARING VIOLATION   Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.3584062 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SHARING VIOLATION   Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.4209304 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SHARING VIOLATION   Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.4834629 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SHARING VIOLATION   Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a


open file.dat
write several records to file.dat
close file.dat 

5:45:28.4899722 PM  APP.exe 1276    CreateFile  C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Desired Access: Generic Read/Write, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: N, ShareMode: Read, Write, AllocationSize: n/a, OpenResult: Opened
5:45:28.4901002 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,236, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4901636 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,236, Length: 28, Priority: Normal
5:45:28.4902365 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,264, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4903031 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,264, Length: 28, Priority: Normal
5:45:28.4903517 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,292, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4905200 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,292, Length: 28, Priority: Normal
5:45:28.4905917 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,320, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4906633 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,320, Length: 28, Priority: Normal
5:45:28.4907120 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,348, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4907747 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,348, Length: 28, Priority: Normal
5:45:28.4908214 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,376, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4908841 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,376, Length: 28, Priority: Normal
5:45:28.4909308 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,404, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4909929 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,404, Length: 28, Priority: Normal
5:45:28.4910396 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,432, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4911023 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,432, Length: 28, Priority: Normal
5:45:28.4911491 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,460, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4912118 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,460, Length: 28, Priority: Normal
5:45:28.4912578 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,488, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4913206 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,488, Length: 28, Priority: Normal
5:45:28.4913673 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,516, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4914300 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,516, Length: 28, Priority: Normal
5:45:28.4914761 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,544, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4915388 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,544, Length: 28, Priority: Normal
5:45:28.4915855 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,572, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4916482 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,572, Length: 28, Priority: Normal
5:45:28.4916936 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,600, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4917570 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,600, Length: 28, Priority: Normal
5:45:28.4918043 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,628, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4919003 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,628, Length: 28, Priority: Normal
5:45:28.4919483 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,656, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4920110 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,656, Length: 28, Priority: Normal
5:45:28.4920577 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,684, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4921205 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,684, Length: 28, Priority: Normal
5:45:28.4921672 PM  APP.exe 1276    QueryStandardInformationFile    C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS AllocationSize: 57,344, EndOfFile: 54,712, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4922299 PM  APP.exe 1276    WriteFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS Offset: 54,712, Length: 28, Priority: Normal
5:45:28.4922843 PM  APP.exe 1276    CloseFile   C:\Users\Public\Documents\APP\Datafile\APP.DAT  SUCCESS

* UPDATE *

I think I have nailed it. I downloaded handle.exe and ran it. Process explorer always shows no handles matching APP.DAT, but the handle.exe utility showed a list of handles owned by PID 4 (System) with APP.DAT as the filename. What I hadn't mentioned in my OP was that the same app runs on another network machine in a secondary "monitor" mode where it doesn't write to the APP*.DAT files but only reads from them. The "aha!" moment was when I realised that the problem only occurred when the monitor app was also running, and that the handle leaks showed as belonging to the "System" process - which presumably created the handle when the file was opened from the network machine.

Occasionally the monitor app was denied access to the file (always with Error 998 - Invalid access to memory location - for some reason) and the error path did not close the handle. From that point on the primary instance of the app was still able to read and write the file APP.DAT but failed on the rename. Making sure the file was closed on the error seems to have fixed it.

The pointer to "handle.exe" was the key, along with the knowledge that I could still write to a file with another opened handle, but a rename would fail. Thanks to everyone for their contributions.

rossmcm
  • 5,493
  • 10
  • 55
  • 118
  • 2
    One possibility is that the realtime virus scanner is getting in the way. I've seen this happen many times. Your sleep should get around it but you may wish to turn off real time scanning to remove it from suspicion.r sleep should get around it but you may wish to turn off real time scanning to remove it from suspicion. – David Heffernan Aug 19 '11 at 07:57
  • I've had virus scanners upset builds, but this is a panel PC with very little extra stuff and no virus checker. – rossmcm Aug 19 '11 at 08:00

5 Answers5

5

A useful way to debug such problems (for me anyway) is to search the file name with the "Find/Find Handle or DLL" function of Process Explorer. If a particular file name is open & exclusive it will reveal which program is holding it. If it is yours, you can check if the handle is getting closed. If not yours, you might have to either just code a small wait (Windows takes its sweet time in getting around to things sometimes), or forcibly unlock the file (I have a little program here that does that with EXPLORER.EXE because it will hold locks on files a lot when I'm using it).

HTH.

Glenn1234
  • 2,542
  • 1
  • 16
  • 21
4

It would appear to me that the problem is not the destination file, but the source file... Make sure the handle is closed on the source file, that's where your problem will be.

Nat
  • 5,414
  • 26
  • 38
  • Hi @Nat. Thanks for looking at this. I have added the log from a Process Monitor session to the question. Yes the issue seems to be with the source file. – rossmcm Aug 19 '11 at 06:39
  • 1
    Are you sure you have closed all the file handles to the source file? – Nat Aug 19 '11 at 06:49
  • Any file accesses would be in the log wouldn't they? - the last access of the file APP.DAT before the failed rename is a successful close. The strange thing is that the write to the file straight after the failed rename attempt is successful. – rossmcm Aug 19 '11 at 07:31
  • 1
    They should be, but you only show snippets of the log, so it's hard to know it has everything in it... My thought is that you might have another handle to the file open in your application, probably with the `fmShareDenyNone` mode... This means that you can still open another handle to it and write to it, but you cannot rename it because it is still open. You'll need to trawl through the log to match all the file opens and closes to make sure they are in pairs. – Nat Aug 19 '11 at 08:38
1

My bet is that your call to GetLastError is returning an error code for an earlier API call unrelated to this routine. As far as I can recall (no source at hand where I am) FileExists doesn't set error code. But if the first call to FileExists fails then you go ahead and call GetLastError anyway.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • +1 Hadn't noticed that. The OP should be doing the get last error call only directly after a routine that reports via it... So it should be directly after the DeleteFile and RenameFile calls not all the way down the bottom. – Nat Aug 19 '11 at 08:51
  • David, @Nat. Granted I've been a bit over-zealous in my GetLastError checking, but the returned error (32) agrees with the ProcMon log (sharing violation) – rossmcm Aug 19 '11 at 12:17
  • I can't see any evidence that you know for sure which API call produces the error code 32. Do you know for sure which it is. It's not hard to work it out. – David Heffernan Aug 19 '11 at 13:21
0

Would it help if you retry the renaming after a second or so? Maybe Windows is too slow to catch up.

Riho
  • 4,523
  • 3
  • 33
  • 48
  • That's a suspicion I've had, and I thought I had seen that very problem mentioned somewhere, but I couldn't find any references to it this time around. I would have thought that 10 times 50 mS would be enough time though? Is there any way I can find out programmatically what is stopping me accessing a file? I assume it's my app (nothing else is running - it's a dedicated Panel PC). – rossmcm Aug 19 '11 at 07:44
  • I don't think that you can get the reason. But just for a test - make the delay much bigger and see if it actually helps. It might actually be loose handle somewhere. BTW, sometimes in Windows you have to actually reboot in order to get the file unlocked :) – Riho Aug 19 '11 at 07:59
  • But the file isn't locked. As the Process Monitor log shows, I can successfully open it, write to it, and close it immediately after the unsuccessful rename. I'll try a longer delay. – rossmcm Aug 19 '11 at 08:25
  • @rossmcm: if process explorer can do it for you (as Glenn1234 suggested), it should be possible to do it programmatically... When we encounter a problem like this, we use a tool called "handle.exe" to log who is holding the file open. Don't know where it came from though, sorry. – Marjan Venema Aug 19 '11 at 08:40
  • The delay probably wont achieve anything. The file system is usually not that slow to close a file handle. And being able to open and read and write to a file does not imply that it is not 'locked' as far as a rename is concerned. – Nat Aug 19 '11 at 08:43
  • The delay would only be useful if another process like AV had the file locked transiently – David Heffernan Aug 19 '11 at 10:59
  • @Nat, David. Increasing the delay from 50mS to 500 mS (and still attempting the rename 10 times) has made no difference. Interestingly, the last time I restarted the app, the first 5 or so renames (at around 10 minute intervals) worked, then all from then on failed. – rossmcm Aug 19 '11 at 12:27
  • @nat CloseHandle is synchronous. – David Heffernan Aug 19 '11 at 14:44
0

It is worth mentioning that .DAT files are scanned by some anti-virus tools. Therefore you may find that you close the file, then the AV opens it to scan it, and while it still has it open, you are trying to operate on it. Of course you cannot as the AV has it open.

mj2008
  • 6,647
  • 2
  • 38
  • 56
  • No virus scanner is installed. – rossmcm Aug 19 '11 at 08:26
  • 1
    What about the Microsoft Malware essentials thing? That bit me while file copying recently. Oh, and why don't you have a virus scanner? To risk shipping your product with a virus would be irresponsible! 8-) – mj2008 Aug 19 '11 at 08:39