Link to home
Start Free TrialLog in
Avatar of phillipf
phillipf

asked on

Shutting down external applications

From a Delphi app, how can I shut down other applications running on the same server.  These other applications do not run as services and are not Delphi Apps.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kretzschmar
damn Barry, fast anyway ;-)

hi phillipf,

from my paq a unit that kills all processes except itself
if you will kill a specific, then you have to compare
with for example the windowtitle

      unit kill_all_u;

      interface

      uses
        Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
        StdCtrls;

      type
        TForm1 = class(TForm)
          Button1: TButton;
          procedure Button1Click(Sender: TObject);
        private
          { Private-Deklarationen }
        public
          { Public-Deklarationen }
        end;

      Function TWCallBack(H : THandle; V : Longint) : longBool; stdcall;

      var
        Form1: TForm1;

      implementation

      {$R *.DFM}

      Function TWCallBack(H : THandle; V : Longint) : longBool; stdcall;
      var
        i,j : Integer;
        canKill : Boolean;
        TID : DWord;

      begin
        CanKill := True;
        if H = application.handle then  //save this app
          CanKill := False
        else
          For J := 0 to screen.FormCount - 1 do
            if screen.Forms[j].Handle = H then
              CanKill := False;
        if CanKill then
        begin
          TID := GetWindowThreadProcessId(H,nil);
          PostThreadMessage(TID,WM_QUIT,0,0);
        end;
        Result := True;
      end;

      procedure TForm1.Button1Click(Sender: TObject);
      var Dummy : Longint;
      begin
        Dummy := 0;
        EnumWindows(@TWCallBack,Dummy);
      end;

      end.

meikl
Avatar of phillipf
phillipf

ASKER

This looks like it will do the trick.  I can find the process just fine but I have not been able to terminate the process yet.

I don't suppose you have a small example of this.

This looks like it will do the trick.  I can find the process just fine but I have not been able to terminate the process yet.

I don't suppose you have a small example of this.

This looks like it will do the trick.  I can find the process just fine but I have not been able to terminate the process yet.

I don't suppose you have a small example of this.

if you know the handle then use this procedure :

procedure KillProcess(window: HWND; timeOut: cardinal);
var pid,ph : THandle;
    c1,c2  : cardinal;
begin
  GetWindowThreadProcessID(window,@pid);
  ph:=OpenProcess(PROCESS_ALL_ACCESS,false,pid);
  if ph<>0 then
    try
      if timeOut>0 then begin
        c1:=GetTickCount;
        PostMessage(window,WM_CLOSE,0,0);
        while (GetTickCount-c1<timeOut) and GetExitCodeProcess(ph,c2) and (c2=STILL_ACTIVE) do begin
          Sleep(10);
          Application.ProcessMessages;
        end;
        if (not GetExitCodeProcess(ph,c2)) or (c2=STILL_ACTIVE) then TerminateProcess(ph,0);
      end else TerminateProcess(ph,0);
    finally CloseHandle(ph) end;
end;


Regards Barry