Wiki Home

Who Has File Lock


Namespace: WIN_COM_API
I have been searching for a solution to this answer for a LONG time.

I need to know the user and process that has a RLOCK() or FLOCK() on a file. I know there is a lock because other users cannot RLOCK() or FLOCK() the particular record or file. The question becomes...who? When the file is opened and locked through a network share, I can see the locks through Computer Management->Shared Folders. This application is running on a terminal server using local drives and not shares. So, I cannot see the file locks via computer management.

How can I tell what process and user has a file locked? What does FLOCK() and RLOCK() actually do with the file? I assume it uses some kind of Windows file locking...right? If so, I assume there is a Windows API to find out who has the lock.

Any help would be appreciated.


Carl,

Thanks, but WhoHasFile.prg only works for files opened under a network share and NOT local files. This is the problem.

[I don't know about you, but I sure can tell who is logged into a PC and uses local files on it.]
[2007.04.10 12:53:00 PM CDT]

Download the file Who Has File.prg from the VFUG web site at http://www.VFUG.Org/Files/WhoHasFile.prg

Carl Warner


This is not as easy as it may sound. Yes, FLOCK() and RLOCK() do use Windows file locking, and yes, it is possible to figure out what process (and thus, what user) has what lock, but you're far better off using some kind of table to keep track of this yourself, because the API is totally undocumented. You could possibly use handle.exe, if you are really desperate, but I don't know what the licensing issues would be there.

By the way, VFP locks one byte near the 2GB limit for each RLOCK(). The actual calulation is (2147483646 - RecNo()). I don't know what FLOCK() locks, but I suspect it's either the very beginning of the file (the entire header, or somewhere in the header), byte 2147483646, or byte 2147483647. -- Peter Crabtree
I usually use File Mon ( http://www.microsoft.com/technet/sysinternals/fileanddiskutilities.mspx ), part of the sysinternls toolkit to spelunk such issues - Lauren Clarke



I have used handle.exe, but it does NOT list the locks. It only lists the files open (unless I am missing something in the output). I don't know if a self maintained table to track this is feasible since the application is very large and there are possibly hundreds of FLOCK() and RLOCK() commands.

I have used File Mon, but I don't know how I would use File Mon to find a lock on a file by another user on terminal server.

Doing some research, it looks like I should use the API calls:
FsRtlAreThereCurrentFileLocks
FsRtlGetNextFileLock

Does this look correct anyone? If so, next problem to solve....how to use these calls. I don't see any examples or info on the FILE_LOCK structure.

Those API calls would work, but they're only callable by kernel-mode drivers... -- Peter Crabtree



It is really mind boggling to me how difficult, if not impossible, it is for a developer like me to find out who has a local file locked.

It seems really un-clean to have the locks put in a DBF each time a RLOCK() or FLOCK() is issued. After hours of research several times, it appears there is not an easy way to find the locks. Bummer.
It should be relatively easy to write a function for who has what file locked.
There are multiple options, one flock database perhaps called flocks.dbf in which each flock is entered and or ended with filename and username (and date/time if you want) or a semaphore file for each locked file, I personally prefer the flocks.dbf solution.
In flocks.dbf you can find/lookup who locks a file.
Depending on what you want to register you can use only 1 record for each locked file uniquely or have a separate record for each lock that occurs.
With rlock this is a bit more complicated because there are much more of them and some users lock multiple records but it can also be done.
The flock problem is relatively simple.

Rob.



##########
Hey if you haven't found an answer to your problem, I know something that may help. It is called Process Explorer it allows you to search for a handle and will show you the thread that has the lock. It will also tell you who is the owner of that thread.

Microsoft bought the company but it should be one of the first couple links returned in google. I used it to find out who had locks on my java rt.jar so that I could reinstall the entire jdk. Turned out some weird process I didn't even expect had it locked.

thanks,
john


This problem arose again for me with a client. I can tell them...yup...the file is locked. But I don't know who has it locked. They aren't too happy to hear me tell them in order to clear the lock, they must get their 25 employees out of the application.
I don't see the locking information on the files using process explorer. Am I just overlooking the lock status somewhere?

[2007.11.08 16:58:00 PM CDT]
>> Thanks, but Who Has File.prg only works for files opened under a network share and NOT local files. This is the problem.

You said the problem was with "local files". Why do 25 users have to exit an application to unlock a "local file"?

Answer - Terminal Server.

I had this issue when developing a C# application recently. It's a PITA there too, because you're using managed code. Anyway, I ended up using HANDLE.EXE from Sysinternals called with switches and parsing the result. Works fine. -- Alan Bourke
( Topic last updated: 2010.04.06 08:02:26 AM )