![]() |
Voller Pfad einer Anwendung anhand Handle
Ich versuche den vollen Pfad einer Anwendung anhand des Handles herauszubekommen. Den Dateinamen kriege ich schon, allerdings nicht den Pfad dazu:
Delphi-Quellcode:
function GetProcessNameFromWnd(hWnd: HWND): string;
var ProcID : DWord; SSHandle : THandle; Continue : boolean; ProcEntry : TProcessEntry32; begin GetWindowThreadProcessID(hWnd, @ProcID); SSHandle := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0); ProcEntry.dwSize := Sizeof(ProcEntry); // gehe alle Prozesse durch Continue := Process32First(SSHandle, ProcEntry); while Continue do begin // gesuchter Prozess (ProcID) in der Liste aller Prozesse gefunden? if ProcEntry.th32ProcessID = ProcID then begin Result := ProcEntry.szExeFile; break; end; Continue := Process32Next(SSHandle, ProcEntry); end; CloseHandle(SSHandle); end; |
Re: Voller Pfad einer Anwendung anhand Handle
Unter welchem OS. Dein Code verhält sich unter ConsumerWindows anders als unter NT ff. Aber
![]() |
Re: Voller Pfad einer Anwendung anhand Handle
Is´ Motzi da? :mrgreen:
![]() |
Re: Voller Pfad einer Anwendung anhand Handle
Delphi-Quellcode:
BTW, das Snapshot-Handle nur auf ungleich INVALID_HANDLE_VALUE abzufragen ist unsicher/falsch, da der Wrapper in der TlHelp32.pas 0 zurückgibt falls die API nicht vorhanden ist.
uses
TlHelp32, PsAPI; function GetWindowProcessImageFileName(Wnd: HWND): string; const DesiredAccess = PROCESS_QUERY_INFORMATION or PROCESS_VM_READ; var ProcessId: DWORD; Snapshot: THandle; ModuleEntry: TModuleEntry32; Process: DWORD; Module: HMODULE; Needed: DWORD; FileName: array [0..MAX_PATH-1] of Char; begin SetLength(Result, 0); ProcessId := 0; GetWindowThreadProcessId(Wnd, ProcessId); if 0 = ProcessId then Exit; // Windows 95, Windows 2000 Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessId); if (Snapshot <> 0) and (Snapshot <> INVALID_HANDLE_VALUE) then try ModuleEntry.dwSize := SizeOf(TModuleEntry32); if Module32First(Snapshot, ModuleEntry) then Result := StrPas(ModuleEntry.szExePath); finally CloseHandle(Snapshot); end else begin // Windows NT 4.0 Process := OpenProcess(DesiredAccess, False, ProcessId); if Process <> 0 then try if EnumProcessModules(Process, PDWORD(Addr(Module)), SizeOf(HMODULE), Needed) and (GetModuleFileNameEx(Process, Module, FileName, SizeOf(FileName)) > 0) then Result := StrPas(FileName); finally CloseHandle(Process); end; end; end; |
Re: Voller Pfad einer Anwendung anhand Handle
Danke für die vielen brauchbaren Antworten! Thx.
|
Re: Voller Pfad einer Anwendung anhand Handle
@Nico: Warum hast du nicht schon eher mal was gesagt? Ich bin der Meinung, Teile meines o.gp. (oben geposteten ;)) Codes stammen aus ohnehin deiner Feder.
|
Re: Voller Pfad einer Anwendung anhand Handle
Zitat:
Delphi hätte dir eigentlich ne Warnung ausgeben müssen, dass der Vergleich immer Wahr ergibt, da ein Cardinal (also THandle) immer ungleich -1 (INVALID_HANDLE_VALUE) ist. ;) mfG mirage228 |
Re: Voller Pfad einer Anwendung anhand Handle
Zitat:
Zitat:
Gruss Nico |
Re: Voller Pfad einer Anwendung anhand Handle
Zitat:
Hab grad ma nachgeschaut, und in Delphi7 ist INVALID_HANDLE_VALUE auch ein Cardinal (ehergesagt, -1 zu DWord gecastet), also ist ein Vergleich damit gültig. mfG mirage228 |
Re: Voller Pfad einer Anwendung anhand Handle
Zitat:
Edit: Ok es ist früh am morgen ^^ ein Blick in die tlhelp32.pas brachte die Erleuchtung zum szExePath. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:22 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