type
PProcessWindow = ^TProcessWindow;
TProcessWindow = record
TargetProcessID: Cardinal;
FoundWindow: hWnd;
end;
function EnumWindowsProc(Wnd: hWnd; ProcWndInfo: PProcessWindow): BOOL;
stdcall;
var
WndProcessID: Cardinal;
begin
GetWindowThreadProc(Wnd, WndProcessID);
if WndProcessID = ProcWndInfo^.TargetProcessID then begin
ProcWndInfo^.FoundWindow := Wnd;
Result := False; // we've already found a window.
end else Result := True; // Keep searching
end;
function GetProcessWindow(TargetProcessID: Cardinal): hWnd;
var
ProcWndInfo: TProcessWindow;
begin
ProcWndInfo.TargetProcessID := TargetProcessID;
ProcWndInfo.FoundWindow := 0;
EnumWindows(@EnumWindowsProc, @ProcWndInfo);
Result := ProcWndInfo.FoundWindow;
end;