views:

1011

answers:

6

I have been trying to figure out how to programmatically identify the process that has a lock on a particular file. I've searched through the Win32 API and WMI, but so far I can't find anything. I know it's possible - Sysinternals is able to list every resource accessed/locked by every process on the system.

Can anyone drop me a hint?

+1  A: 

Because of the way Process Explorer works, I suspect that what you need to look for is a way of finding the file handles attached to a given process, and that you'll have to pull that list for each process in the system and look for your file within it.

chaos
+4  A: 

It's a Win32 FAQ (handle => PID)

See news://comp.os.ms-windows.programmer.win32 for the code (C/C++)

Try making your URL a link, too!
Brian
`news://` links get filtered out by the code.
Brad Gilbert
A: 

I don't know in Windows, but somebody might find useful to know that, in Linux, you can use the lsof command, or just search through the folders /proc/PROCESS_ID/fd and see what process has opened the file.

Marc
On Linux this is not much of a problem since inodes are refcounted, so you can remove a file even if someone is holding it open.
JesperE
A: 

Please look at http://windowsxp.mvps.org/processlock.htm

lakshmanaraj
A: 

WhoLockMe is a nice right click windows explorer extension.

AndreasN
A: 

This article might be helpful to you.

It appears you are forced to search through the list of files for each process on the system using undocumented functions in ntdll.dll.

Mathias