Mirror

How to close another application (Views: 710)


Problem/Question/Abstract:

How to close another application

Answer:

library KillGUI;

uses
  Windows, Messages;

function PostQM(nCode: Integer; wParam: WParam; lparam: LParam): Lparam; stdcall;
begin
  PostQuitMessage(1);
  Result := 0;
end;

function ExitRP(nCode: Integer; wParam: WParam; lparam: LParam): Lparam; stdcall;
begin
  ExitProcess(1);
  Result := 0;
end;

procedure PostQuit(AHandle: THandle; Level: DWord); stdcall;
var
  tid: DWord;
  pid: DWord;
  hProcess: THandle;
begin
  tid := GetWindowThreadProcessId(AHandle, @pid);
  case Level of
    0:
      PostMessage(AHandle, WM_CLOSE, 0, 0);
    1:
      SetWindowsHookEx(WH_GETMESSAGE, PostQM, Hinstance, tid);
    2:
      SetWindowsHookEx(WH_GETMESSAGE, ExitRP, Hinstance, tid);
    3:
      begin
        hProcess := OpenProcess(PROCESS_TERMINATE, False, pid);
        TerminateProcess(hProcess, 1);
      end;
  end;
  PostThreadMessage(tid, 0, 0, 0);
end;

exports
  PostQuit name 'PostQuit';

begin
end.

<< Back to main page