How To Get the exe name from a Window Handle

How to get the exe file name when given a window handle.

uses ..., enumStuff;

function WindowToExe(window: dword) : string;
var pid : dword;
    pl : TProcessList;
    i1 : integer;
begin
  result := '';
  pl := nil;
  GetWindowThreadProcessID(window, @pid);
  if pid <> 0 then begin
    pl := GetProcessList;
    for i1 := 0 to high(pl) do
      if pl[i1].pid = pid then begin
        result := pl[i1].name;
        break;
      end;
  end;
end;

----------
Two notes:
(1) My unit "enumStuff" is free, even for commercial usage.
(2) This solution works in all current (win95, win95osr2, win98, win98se, winME, winNT4, win2k and winXP) and probably also in all (near) future Microsoft OSs.

 

Share this article!

Follow us!

Find more helpful articles: