function uhfGetWindowModuleFileName(hWindow:THandle): string;
type
TGetModuleFileNameEx=function(hProcess:THandle;hMod:THandle;szBaseName:PChar;iSize:Integer):Bool;stdcall;
TEnumProcessModules=function(hProcess:THandle;var hMod:THandle;cSize:Cardinal;var cSizeOut:Cardinal):Bool;stdcall;
var
cProcID: DWORD;
hSSHandle: THandle;
bContinue: Boolean;
rProcEntry: TProcessEntry32;
hProcHandle: THandle;
szName: array[0..1024] of Char;
GetModuleFileNameEx: TGetModuleFileNameEx;
EnumProcessModules: TEnumProcessModules;
hModPSAPI: THandle;
hMod: THandle;
cDummy: Cardinal;
begin
result:='';
If Win32Platform = VER_PLATFORM_WIN32_NT then
begin
hModPSAPI := LoadLibrary('psapi.dll');
if hModPSAPI <> 0 then
begin
GetModuleFileNameEx := GetProcAddress(hModPSAPI, 'GetModuleFileNameExA');
EnumProcessModules := GetProcAddress(hModPSAPI, 'EnumProcessModules');
if Assigned(GetModuleFileNameEx) and Assigned(EnumProcessModules) then
begin
if GetWindowThreadProcessID(hWindow, @cProcID) <> 0 then
begin
hProcHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, cProcID);
if hProcHandle <> 0 then
begin
try
if EnumProcessModules(hProcHandle, hMod, sizeof(hMod), cDummy)then
if GetModuleFileNameEx(hProcHandle, 0, @szName, sizeof (szName))then
Result := StrPas(szName)
finally
CloseHandle(hProcHandle)
end;
end;
end;
end;
FreeLibrary(hModPSAPI);
end;
end
else begin
if GetWindowThreadProcessID(hWindow, @cProcID) <> 0 then
begin
hSSHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSSHandle <> INVALID_HANDLE_VALUE) then
begin
rProcEntry.dwSize := Sizeof(rProcEntry);
bContinue := Process32First(hSSHandle, rProcEntry);
while bContinue do
begin
if rProcEntry.th32ProcessID = cProcID then
begin
Result := rProcEntry.szExeFile;
Break;
end;
bContinue := Process32Next(hSSHandle, rProcEntry);
end;
CloseHandle(hSSHandle);
end;
end;
end;
end;