//set a new function
function GetHwndFromProcess(ProcessId: DWORD): HWND;
function _EnumWindowsProc(P_HWND: Cardinal; lParam: Cardinal): Boolean; stdcall;
var
PID: DWORD;
begin
GetWindowThreadProcessId(P_HWND, @PID);
if PCardinal(lParam)^ <> PID then
Result := True
else
begin
Result := False;
PCardinal(lParam+4)^ := P_HWND;
end;
end;
var
Buffer: array[0..1] of Cardinal;
begin
Result := 0;
Buffer[0] := ProcessId;
Buffer[1] := 0;
EnumWindows(@_EnumWindowsProc, Integer(@Buffer));
if Buffer[1] > 0 then Result := Buffer[1];
end;
HWND GetWindowHandleByPID(DWORD dwProcessID)
{
HWND h = GetTopWindow(0 );
while ( h )
{
DWORD pid = 0;
DWORD dwTheardId = GetWindowThreadProcessId( h,&pid);
if (dwTheardId != 0)
{
if ( pid == dwProcessID/*your process id*/ )
{
// here h is the handle to the window
return h;
}
}
h = GetNextWindow( h , GW_HWNDNEXT);
}
return NULL;
}