Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi prozess-handle --> dateiname (https://www.delphipraxis.net/18343-prozess-handle-dateiname.html)

dde 16. Mär 2004 22:37


prozess-handle --> dateiname
 
wie kann ich durch den prozess-handle den dateinamen des prozesses herausbekommen?

SirThornberry 16. Mär 2004 22:40

Re: prozess-handle --> dateiname
 
kleiner Auszug aus nem Source
Delphi-Quellcode:
uses .., tlhelp32, .., ..;

procedure TProcessList.GetHandleInfo(WinHandle: HWND; var akttask: TTask);
var dwActiveProcessId,hModuleSnap:DWORD;snap:THandle;pe32,pe2:TPROCESSENTRY32;me32:TMODULEENTRY32;
Begin
 GetWindowThreadProcessId(WinHandle, @dwActiveProcessId);
 Snap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
 if Process32First(Snap, pe32) = true then
 begin
  if pe32.th32ProcessID = dwActiveProcessId then pe2 := pe32;
  while Process32Next(Snap, pe32) = true do if pe32.th32ProcessID = dwActiveProcessId then pe2 := pe32;
 end;
 akttask.ExeFile := pe2.szExeFile;
[...]

NicoDE 16. Mär 2004 23:13

Re: prozess-handle --> dateiname
 
Zitat:

Zitat von dde
wie kann ich durch den prozess-handle den dateinamen des prozesses herausbekommen?

Das ist nicht gerade trivial. Die Prozess-ID wäre wesentlich hilfreicher.

Ab Windows XP gäbe es diese Möglichkeit...
(hat den Vorteil, dass man 'nur' PROCESS_QUERY_INFORMATION benötigt - statt PROCESS_QUERY_INFORMATION und PROCESS_VM_READ für GetModuleFilenameEx)
Delphi-Quellcode:
// requires PROCESS_QUERY_INFORMATION (and Windows XP)
function GetProcessImageFileName(Process: THandle): string;
type
  TFNGetProcessImageFileNameA = function(Process: THandle; ImageFileName: LPSTR;
    Size: DWORD): DWORD; stdcall;
const
  Size = MAX_PATH;
var
  PsApiModule: HMODULE;
  GetProcessImageFileNameA: TFNGetProcessImageFileNameA;
begin
  Result := '';
  PsApiModule := LoadLibrary('psapi.dll');
  if (PsApiModule <> 0) then
  try
    GetProcessImageFileNameA := TFNGetProcessImageFileNameA(
      GetProcAddress(PsApiModule, 'GetProcessImageFileNameA'));
    if not Assigned(GetProcessImageFileNameA) then
      SetLastError(ERROR_CALL_NOT_IMPLEMENTED)
    else
    begin
      SetLength(Result, Size + 1);
      SetLength(Result, GetProcessImageFileNameA(Process, PChar(Result), Size));
    end;
  finally
    FreeLibrary(PsApiModule);
  end;
end;
...wobei das Ergebnis möglicherweise nicht ganz Deinen Erwartungen entsprechen wird (zum Beispiel: '\Device\HarddiskVolume6\borland\Delphi6\Projects\ Project1.exe')


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:34 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz