根据文件文件句柄或进程ID获取文件名

减小字体 增大字体 作者:佚名  来源:转载  发布时间:2011-02-17 10:42:48

DWORD GetModuleFileName(

  HMODULE HMODULE, // handle to module to find filename for
  LPTSTR lpFilename, // pointer to buffer for module path
  DWORD nSize // size of buffer, in characters
  );

function GetProcessFileName(processID: DWORD): string;
var
  fn: array [0 .. MAX_PATH - 1] of char;
  hProcess: THANDLE;
  hMod: array [0 .. 1023] of HMODULE;
  cbNeeded, size: DWORD;
  i: DWORD;
  p: PProcessRec;
begin
  Result := '';
  hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE,
    processID);
  if (hProcess <> 0) { and (hProcess <> $FFFFFFFF) } then
  begin
    if (EnumProcessModules(hProcess, @hMod, sizeof(hMod), @cbNeeded)) then
    begin
      size := cbNeeded div sizeof(HMODULE);
      GetModuleFileNameEx(hProcess, hMod[0], fn, sizeof(fn));
      Result := fn;
      if Pos('\??\', Result) = 1 then
        Delete(Result, 1, 4)
      else if Length(Result) > 3 then
        if (Result[1] = '\') and (Result[2] <> '\') then
        begin
          Result[1] := '%';
          i := Pos('\', Result);
          if i > 0 then
            Insert('%', Result, i);
        end;
      ExpandFilePath(Result);
    end;
  end;
  CloseHandle(hProcess);
end;

Tags:

作者:佚名
  • 好的评价 如果您觉得此文章好,就请您
      0%(0)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论