Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Win32 API can be used to find the process that has a given file open?

Tags:

file

winapi

api

If I read or write to a file and receive a 33 or 32 error, I would like to log a message containing the name of the other process(es) that have the file opened. There must be a Win32 API I could use to get this info. Process Explorer displays it. Of course, Process Explorer also has info about all processes in memory. I'd prefer to find the culprit without interrogating all processes.

like image 898
Jeremy Mullin Avatar asked Oct 08 '08 17:10

Jeremy Mullin


People also ask

How do you find who has a file open in Windows?

You can find the Winfile file in the \%systemroot%\system32 directory. Browse to the file you want (even across a network share). Press Alt+Enter to view the file's properties. Click Open by to determine who has the file open.

What is Win32 API used for?

Alternatively referred to as the Windows API and WinAPI, Win32 is the main set of Microsoft Windows APIs used for developing 32-bit applications. These APIs are responsible for functions in the following categories: Administration and Management - Install, configure, and service applications or systems.

On which operating system could you use the Win32 API?

Win32 APIs exist for many features and technologies in Windows 10, including core user interface and windowing APIs, audio and graphics, and networking.


2 Answers

The Handle program from SysInternals does this as well, but I'm not sure how. I don't think there is a single documented API to do this. I think you have to iterate over the processes, enumerate each of the handles in the process, and then determine the filename associated with that handle.

like image 183
Nick Avatar answered Dec 11 '22 06:12

Nick


As of Windows Vista, the Restart Manager can be used to determine which process(es) have a file open.

This page has sample code: https://devblogs.microsoft.com/oldnewthing/20120217-00/?p=8283

From that page, the steps (for the sample code) are the following:

  1. Create a Restart Manager session.
  2. Add a file resource to the session.
  3. Ask for a list of all processes affected by that resource.
  4. Print some information about each process.
  5. Close the session.

Here is the documentation for the Restart Manager: https://learn.microsoft.com/en-us/windows/win32/rstmgr/restart-manager-portal

like image 38
David Avatar answered Dec 11 '22 06:12

David