2
votes

I have not worked so much with files: I am wondering about possible issues with accessing remote files on another computer. What if the distant application crashes and doesn't close the file ?

My aim is to use this win32 function: HFILE WINAPI OpenFile(LPCSTR lpFileName, LPOFSTRUCT lpReOpenBuff, UINT uStyle);

Using the flag OF_SHARE_EXCLUSIVE assures me that any concurrent access will be denied (because several machines are writing to this file from time to time).

But what if the file is left open ? (application crash for example ?) How to put the file back to normal ?

2

2 Answers

3
votes

What if the distant application crashes and doesn't close the file ?

Then the O/S should close the file when it cleans up after the "crashed" application.

This won't help with a "hung" application (an application which stays open but does nothing forever).

I don't know what the issues are with network access: for example if the network connection disappears when a client has the file open (or if the client machine switches off or reboots). I'd guess there are timeouts which might eventually close the file on the server machine, but I don't know.

It might be better to use a database engine instead of a file: because database engines are explicitly built to handle concurrent access, locking, timeouts, etc.

2
votes

I came across the same problem using VMware, which sometimes doe not release file handles on the host, when files are closed on the guest.

You can close such handles using the handle utility from www.sysinternals.com

First determine the file handle id my passing a part of the filename. handle will show all open files where the given string matches a part of the file name:

D:\sysinternals\>handle myfile
deadhist.exe       pid: 744     3C8: D:\myfile.txt

Then close the hanlde using the parameters -c and -p

D:\sysinternals\>handle -c 3c8 -p 744
  3C8: File  (---)   D:\myfile.txt
Close handle 3C8 in LOCKFILE.exe (PID 744)? (y/n) y

Handle closed.

handle does not care about the application holding the file handle. You are now able to reopen, remove, rename etc. the file