Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

GetFinalPathNameByHandle API Hangs

3.86/5 (3 votes)
8 Apr 2009CPOL1 min read 29.7K  
GetFinalPathNameByHandle is an new API introduced in Windows Vista. We will look at how to resolve an issue related to its usage.

GetFinalPathNameByHandle is a new API introduced in Windows Vista. As you can read from MSDN, the purpose of this API is to return the file name corresponding to a file handle.

Some months back, I wrote a small utility, OpenedFileFinder, which lists all the files that are opened under a certain folder. While writing this application, the main problem I came across was to get the file name from the handle. Initially, I used a function called NtQueryObject(). But the problem with this API is that if we try to query the details for a certain object such as a pipe, directory handle etc., it will hang. So, in my application, I introduced a driver to get around this hanging problem. When I found the new GetFinalPathNameByHandle(), I was so happy and I thought at least I could avoid the driver in Vista. I tried the API, but came to find that this API also has the same problem described earlier.

It seems the GetFinalPathNameByHandle also uses the same NtQueryObject() to get the file name and so it hangs. Any how, I reported this bug to Microsoft Connect. Let us see how they solve the problem.

Another API that is worth mentioning here is GetFileInformationByHandleEx(), which is also a new API introduced in Vista. Using this API, I can get the file name from a handle. So I decided to check whether the same problem exists in this API. Surprisingly, it worked fine. And in MSDN, it is mentioned that it uses a driver for attaining the task. So I highly recommend using the GetFileInformationByHandleEx() API instead of GetFinalPathNameByHandle().

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)