I slightly adapted the code from here:
Delphi – finding the process that is accessing a file from my program
to return the list of handle names by a given pID, I add every entry to a TStringList. The issue is the names contain garbage like “?” in filenames which contain unicode characters, for example “xxx ★ 5” comes up as “xxx ? 5”
What is wrong in the code?
Seeing a question mark for any non-ASCII character is the hallmark of an attempt to convert from Unicode to ANSI.
The problem is in this function:
This converts from Unicode to ANSI in the calls to
WideCharToMultiByte. You simply need to remove that part of the code.You’ll need to modify this record
so that
FileNameis an array ofWideChar.And then adapt
GetFileNameHandleThraccordingly. I’ve not studied it in detail, but I expect you need something like this:I’ve not run this code so you may need to work on it further. However, it’s obvious that the problem is down to the conversion to ANSI.I’ve now run this code and it works well.