visual-c++visual-c++-6filehandleopenprocess

How to find & close all open file handles on a removable drive (before ejecting)


I'm trying to eject a (virtual) removable drive, and it fails because there are some file handles open, maybe explorer windows. What's the best way to get all the open handles to files on that drive and close them?

Is it also feasible (within user-mode) to find any processes running off that drive, so I can warn the user with the process name?

Ideally I'd like to do this within a user-mode app rather than require admin privileges.

I'm using VC++ in Visual Studio 2005, in an MFC app, but samples in any language would be welcome!


Solution

  • The Process Explorer utility can search for open handles, but I don't know whether it can do so without elevated privileges. From what I've been able to gather, it does this using the "Performance Counters" API.

    I think this article does what you need, enumerate the running processes, enumerate the handles for each one, look for a file handle referencing a path on the drive in question:

    How to enumerate process' handles?