0

I am developing a C++ application on Windows server and I am trying to find ways to obtain which files are open by other processes. My current solution is inspired by code from the Chromium project. I iterate over all processes and obtain their file handles, but this performs poorly.

Is there another API function that lets me reverse this lookup by having a file path instead and obtaining the process handle that has a file handle open?

HelloWorld
  • 2,392
  • 3
  • 31
  • 68
  • Do you think the Chromium developers are not good enough to find the better solution than that you saw in the Chromium code? If you see something in Chromium useful for you, you can be pretty sure there is no a better solution than that in Chromium. – 273K May 26 '21 at 16:03
  • They go with that approach because they need it this way. My requirements are completely different. Our use-cases only have intersections (file-handles). That's why I wrote I need it the other way around – HelloWorld May 26 '21 at 16:25
  • 1
    @S.M. - this concrete code - very not the best – RbMm May 26 '21 at 16:25
  • 2
    are you looking for `FileProcessIdsUsingFileInformation` ? something like [this](https://stackoverflow.com/questions/47507578/winapi-get-the-process-which-has-specific-handle-of-a-file) ? – RbMm May 26 '21 at 16:29
  • [How do I find out which process has a file open?](https://devblogs.microsoft.com/oldnewthing/20120217-00/?p=8283) – IInspectable May 26 '21 at 16:38
  • @IInspectable- this way very not efficient compare to `FileProcessIdsUsingFileInformation ` – RbMm May 26 '21 at 16:40
  • Not efficient by what metric? I'm sure that code using a public, documented API can more efficiently be maintained by developers on your team than something that's listed as *"[This value is reserved for system use.](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ne-wdm-_file_information_class)"*. Developer time is a resource, and frequently the true bottleneck. – IInspectable May 26 '21 at 16:48
  • 1
    *Not efficient by what metric* - by performs. if something *reserved for system use* this not mean that not possible use this – RbMm May 26 '21 at 16:56
  • @RbMm Thanks for your answers! I will check that out – HelloWorld May 26 '21 at 17:14
  • 1
    Sure, it is *possible* to use something the vendor of the OS suggests you do not use. It's just a lot harder to research how to use, and it doesn't come with any implied or expressed guarantees. Side-stepping contracts is almost always possible. For most developers it just isn't one of the first things to consider when looking for a solution. Much like most people won't consider robbing banks, even though the benefits are very attractive and well understood. – IInspectable May 26 '21 at 17:44

0 Answers0