Or as a function:
Shane
function GetExePath(WH : HWND): String;
var
dwActiveProcessId : DWORD;
Snap : THandle;
pe32 : TPROCESSENTRY32;
Begin
result:= '';
GetWindowThreadProcessId(W
try
Snap := CreateToolhelp32Snapshot(T
dwActiveProcessId);
if Snap <> 0 then
begin
if Process32First(Snap, pe32) then
begin
if pe32.th32ProcessID = dwActiveProcessId then
begin
result := String(pe32.szExeFile);
end
else
begin
while Process32Next(Snap, pe32) do
begin
if pe32.th32ProcessID = dwActiveProcessId then
begin
Result := String(pe32.szExeFile);
Break;
end; //of: if pe32.th32ProcessID = dwActiveProcessId
end; //of while
end; //of else
end; //of: if Process32First(Snap, pe32)
end; //of: if Snap <> 0
finally
CloseHandle(Snap);
end;
end;
Main Topics
Browse All Topics





by: shaneholmesPosted on 2004-04-26 at 08:42:05ID: 10919499
You can use GetModuleFileName to retrieve the name of an EXE from a
H, @dwActiveProcessId ); H32CS_SNAP PROCESS,
module handle. However, using GetModuleFilename only works on Win32 if the window
is part of your own process.
You need to use Toolhelp32 functions (on Win95
only) or PSAPI.DLL (NT 4) to get from a process ID (which you obtain from the
window handle via GetWindowthreadProcessID) to the filename of the EXE.
Delphi
encapsulates Toolhelp32 in the TlHelp32 unit.
procedure GetExePath(WH : HWND; var Exepath : string);
var
dwActiveProcessId : DWORD;
Snap : THandle;
pe32 : TPROCESSENTRY32;
Begin
GetWindowThreadProcessId(W
try
Snap := CreateToolhelp32Snapshot(T
dwActiveProcessId);
if Snap <> 0 then
begin
if Process32First(Snap, pe32) then
begin
if pe32.th32ProcessID = dwActiveProcessId then
begin
ExePath := String(pe32.szExeFile);
end
else
begin
while Process32Next(Snap, pe32) do
begin
if pe32.th32ProcessID = dwActiveProcessId then
begin
ExePath := String(pe32.szExeFile);
Break;
end; //of: if pe32.th32ProcessID = dwActiveProcessId
end; //of while
end; //of else
end; //of: if Process32First(Snap, pe32)
end; //of: if Snap <> 0
finally
CloseHandle(Snap);
end;
end;
Shane