Naar aanleiding van deze discussie ben ik gaan zoeken en combineren, en onderstaande code was het resultaat. De code is getest onder Windows XP, en volgens de documentatie zou deze werkwijze moeten compenseren voor alle systemen. Als iemand dit heeft getest onder andere systemen, laat 't me even weten via een PM, dan hebben anderen daar ook weer wat aan!
Code:
uses
Windows,
TlHelp32,
PsAPI;
function GetWindowPath(const AWindow: THandle): String;
var
dProcess: THandle;
hProcess: THandle;
hSnapshot: THandle;
pProcess: TProcessEntry32;
hModule: THandle;
dNeeded: Cardinal;
cPath: array[0..MAX_PATH] of Char;
begin
Result := '';
// Get window process
GetWindowThreadProcessId(AWindow, dProcess);
// On the Windows NT series, the ToolHelp32 functions don't return the
// full path. We therefore rely on GetModuleFileNameEx, which is not
// available on 9x/ME.
if Win32Platform = VER_PLATFORM_WIN32_NT then begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
False, dProcess);
if hProcess <> 0 then begin
// Get first module in process
if EnumProcessModules(hProcess, @hModule, SizeOf(hModule), dNeeded) then begin
FillChar(cPath, SizeOf(cPath), #0);
if GetModuleFileNameEx(hProcess, hModule, @cPath, SizeOf(cPath)) > 0 then
Result := String(cPath);
end;
CloseHandle(hProcess);
end;
end else begin
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
// The ToolHelp32 functions are dynamically loaded, and the wrappers will
// return 0 when they are not present on the system.
pProcess.dwSize := SizeOf(pProcess);
if (hSnapshot <> 0) and (hSnapshot <> INVALID_HANDLE_VALUE) then
if Process32First(hSnapshot, pProcess) then
repeat
if pProcess.th32ProcessID = dProcess then begin
Result := String(pProcess.szExeFile);
break;
end;
until not Process32Next(hSnapshot, pProcess);
end;
end;
function GetActiveWindowPath(): String;
begin
Result := GetWindowPath(GetForegroundWindow());
end;
Update: vergeten units toe te voegen die je nodig hebt. Ze staan nu bij "uses" . Bedankt SVG!
Bookmarks