How to determine the process of accessing it according to the file name

MuYu 41 Reputation points
2021-11-29T07:52:37.103+00:00

I want to implement a function that can determine the process accessing it according to the file name。
I found two implementation methods on the network。

1.Use the NtQuerySystemInformation function to find all handles, then filter out file handles from all handles and get the file name.
--However, the instructions for SystemHandleInformation were not found in the NtQuerySystemInformation API doc。
--There seems to be no definition in the SDK header file
153272-image.png
153226-image.png

2.It seems that I can use Restart Manager to register the file name and get the name of the process or service accessing the file to achieve this effect.
--But I seem to need to use the dynamic link library file rstrtmgr.dll,
--I don't have this DLL in my system, and I can't find a DLL that can be used. I once downloaded one, but when I use it, I am prompted that with "skipping incompatible.. / rstrtmgr.dll when searching for - lrstrtmgr"

Finally, I found the handle.exe tool in Doc / sysinternals, but I don't know how to achieve this effect.

Or there are other ways to enumerate the handles of running processes.
Can someone help me……

Thank you very much! ☆⌒(´▽`)v THX!!

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,426 questions
Sysinternals
Sysinternals
Advanced system utilities to manage, troubleshoot, and diagnose Windows and Linux systems and applications.
1,092 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 81,741 Reputation points
    2021-11-29T08:13:48.777+00:00

    You can use IFileIsInUse and GetInfoForFileInUse
    I had posted a sample in VB in this thread : How to kill the process which is accessing an image file, so I can delete it in WinForms App.
    For SystemHandleInformation, I had posted a sample in C++ in this thread : Finding process holding device reference

    1 person found this answer helpful.

  2. Xiaopo Yang - MSFT 11,496 Reputation points Microsoft Vendor
    2021-12-02T05:54:03.343+00:00

    I also found two answers. 1. How to find all processes using a device 2. Get the process which has specific handle of a file(using NtQueryInformationFile, a little tricky)