Jach Many Jach Many - 22 days ago 7
C Question

How to write a program like Unlocker

Recently I was trying to delete a file, thinking that I had closed the program manipulating the file, but it could not be deleted because the program was still running. So I used Unlocker to find the process which opened that file.

I would like to write a program in C or .NET to mimic this process. How can I find out which process has a handle to the file and then close this handle?

Some references to relevant Windows API functions would be nice.

Answer

You need to create a device driver for this that will walk the handle table of each process in the system and return the list of open handles (and what they are handles to).

This is a non-trivial task that is very easy to get wrong. For example, even the last version of Process Explorer (which has the same functionality, BTW) had a bug in this path that led to a system crash.

-scott