program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils, Windows, Messages;
//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;
function GetWindowHandleByPID(dwProcessID: DWORD): Cardinal;
var
Lh: HWND;
Lpid: DWORD;
LdwTheardId: DWORD;
begin
Result := 0;
Lh := GetTopWindow(0);
while Lh <> 0 do
begin
Lpid := 0;
LdwTheardId := GetWindowThreadProcessId(Lh, Lpid);
if (LdwTheardId <> 0) then
begin
if ( Lpid = dwProcessID{your process id}) then
begin
// here h is the handle to the window
Result := Lh;
Exit;
end;
end;
Lh := GetNextWindow(Lh, GW_HWNDNEXT);
end;
end;
var
LHandle: HWND;
LPid: Cardinal;
begin
try
//LHandle := GetHwndFromProcess(2448);
LPid := StrToIntDef(ParamStr(1), 0);
if LPid <> 0 then
begin
LHandle := GetWindowHandleByPID(LPid);
SendMessage(LHandle, WM_CLOSE, 0, 0);
end;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.