Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9129161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:40:58+00:00 2026-06-17T07:40:58+00:00

I slightly adapted the code from here: Delphi – finding the process that is

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:40:59+00:00Added an answer on June 17, 2026 at 7:40 am

    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:

    function GetFileNameHandleThr(Data: Pointer): DWORD; stdcall;
    var
      dwReturn: DWORD;
      FileNameInfo: FILE_NAME_INFORMATION;
      ObjectNameInfo: TOBJECT_NAME_INFORMATION;
      IoStatusBlock: IO_STATUS_BLOCK;
      pThreadParam: TGetFileNameThreadParam;
    begin
      ZeroMemory(@FileNameInfo, SizeOf(FILE_NAME_INFORMATION));
      pThreadParam := PGetFileNameThreadParam(Data)^;
      Result := NtQueryInformationFile(pThreadParam.hFile, @IoStatusBlock,  @FileNameInfo, MAX_PATH * 2, FileNameInformation);
      if Result = STATUS_SUCCESS then
      begin
        Result := NtQueryObject(pThreadParam.hFile, ObjectNameInformation,  @ObjectNameInfo, MAX_PATH * 2, @dwReturn);
        if Result = STATUS_SUCCESS then
        begin
          pThreadParam.Result := Result;
          WideCharToMultiByte(CP_ACP, 0, @ObjectNameInfo.Name.Buffer[ObjectNameInfo.Name.MaximumLength - ObjectNameInfo.Name.Length], ObjectNameInfo.Name.Length, @pThreadParam.FileName[0], MAX_PATH, nil, nil);
        end
        else
        begin
          pThreadParam.Result := STATUS_SUCCESS;
          Result := STATUS_SUCCESS;
          WideCharToMultiByte(CP_ACP, 0, @FileNameInfo.FileName[0], IoStatusBlock.Information, @pThreadParam.FileName[0], MAX_PATH, nil, nil);
        end;
      end;
      PGetFileNameThreadParam(Data)^ := pThreadParam;
      ExitThread(Result);
    end;
    

    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

    TGetFileNameThreadParam = packed record
      hFile    : THandle;
      Result   : NT_STATUS;
      FileName : array [0..MAX_PATH - 1] of AnsiChar;
    end;
    

    so that FileName is an array of WideChar.

    TGetFileNameThreadParam = packed record
      hFile    : THandle;
      Result   : NT_STATUS;
      FileName : array [0..MAX_PATH - 1] of WideChar;
    end;
    

    And then adapt GetFileNameHandleThr accordingly. I’ve not studied it in detail, but I expect you need something like this:

    function GetFileNameHandleThr(Data: Pointer): DWORD; stdcall;
    var
      dwReturn: DWORD;
      FileNameInfo: FILE_NAME_INFORMATION;
      ObjectNameInfo: TOBJECT_NAME_INFORMATION;
      IoStatusBlock: IO_STATUS_BLOCK;
      pThreadParam: TGetFileNameThreadParam;
    begin
      ZeroMemory(@FileNameInfo, SizeOf(FILE_NAME_INFORMATION));
      pThreadParam := PGetFileNameThreadParam(Data)^;
      Result := NtQueryInformationFile(pThreadParam.hFile, @IoStatusBlock,  @FileNameInfo, MAX_PATH * 2, FileNameInformation);
      if Result = STATUS_SUCCESS then
      begin
        Result := NtQueryObject(pThreadParam.hFile, ObjectNameInformation,  @ObjectNameInfo, MAX_PATH * 2, @dwReturn);
        if Result = STATUS_SUCCESS then
        begin
          pThreadParam.Result := Result;
          Move(ObjectNameInfo.Name.Buffer[ObjectNameInfo.Name.MaximumLength - ObjectNameInfo.Name.Length], pThreadParam.FileName[0], Min(ObjectNameInfo.Name.Length, MAX_PATH)*SizeOf(WideChar));
          //WideCharToMultiByte(CP_ACP, 0, @ObjectNameInfo.Name.Buffer[ObjectNameInfo.Name.MaximumLength - ObjectNameInfo.Name.Length], ObjectNameInfo.Name.Length, @pThreadParam.FileName[0], MAX_PATH, nil, nil);
        end
        else
        begin
          pThreadParam.Result := STATUS_SUCCESS;
          Result := STATUS_SUCCESS;
          Move(FileNameInfo.FileName[0], pThreadParam.FileName[0], Min(IoStatusBlock.Information, MAX_PATH)*SizeOf(WideChar));
          //WideCharToMultiByte(CP_ACP, 0, @FileNameInfo.FileName[0], IoStatusBlock.Information, @pThreadParam.FileName[0], MAX_PATH, nil, nil);
        end;
      end;
      PGetFileNameThreadParam(Data)^ := pThreadParam;
      ExitThread(Result);
    end;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I borrowed the HMAC-SHA1 Java code from https://www.rfc-editor.org/rfc/rfc6238 and adapted slightly to hardcode it
A slightly archaic question I'm afraid but here goes: I have a program which
I was surprised that the Latex code from a recent question didn't throw up
I slightly remember from my age old Java days, that there was an RequestScope
I slightly remember from age old PHP days (years ago) that different functions wanted
slightly different question about variance this time. I take it from experimentation that C#
I am developing an ASPX file that returns results from a SQL stored proc
I'm trying to extract two blocks of code from a text file using Java
Ok found some great code on Stack. Have adapted it slightly. But in essence
Slightly unusual requirement here, which unfortunately is down to a poor table design a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.