Link to home
Start Free TrialLog in
Avatar of Dark_King
Dark_King

asked on

Kill Program ?

How do I kill a Another program from delphi.

Like ctrl+alt+del Do
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 inthe
inthe

MyHandle could also := FindWindow(TopWindowsclassname, nil);

MyHandle := FindWindow('notepad', nil);
 
Avatar of Dark_King

ASKER

Adjusted points to 40
I tried this but don work’s
My program has not a visible windows

procedure TForm1.Button1Click(Sender: TObject);
var
  MyHandle: THandle;
begin

begin
  MyHandle := FindWindow(nil, 'WipOff');
  if MyHandle <> 0 then
  begin
    PostMessage(MyHandle, WM_QUIT, 0, 0);
    //or if you want to give the window a chance to confirm the close do this
    //PostMessage(MyHandle, WM_CLOSE, 0, 0);
  end;
end;


end;
then youll probably need to use its classname.
(did you create this program?if so it helps as it means the classname will be whatever you named the form with a "T" in front.for instance if your form is named MyGreatForm then
MyHandle := FindWindow(TMyGreatForm, 'nil');
also if your window is not a top level window(though it would need to be to close down the app)you may need findwindowex()

search yahoo for a program called "sinfo" you can use this to tell you the classnames of programs you didnt write .
but for a more generic solution to get a proper handle to any program as windows does in ctr-al-del then you will need to get the enumstuff from madshis website:
http://nettrash.com/users/madshi/
and use openprocess()etc ..
if you do know or can get correct class name something like this is another solution:

procedure TForm1.Button1Click(Sender: TObject);
var
h : thandle;
begin
h := findwindow('notepad',nil);
edit1.text := inttostr(h);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
wnd2 : hwnd;
ProcessID,cpid : Cardinal;
begin
wnd2:=strtoint(edit1.text);
ProcessID := GetWindowThreadProcessID(wnd2,@cpid);
TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,cpid),1);
end;
and finally example using madshi's enumstuff unit:

uses Enumstuff;

Procedure Killapp(exeFile: string);
var pl : TProcessList;
    i1 : integer;
     h : thandle;
begin
  pl:=GetProcessList;
  for i1:=0 to high(pl) do
    if CompareText(ExtractFileName(pl[i1].name),ExtractFileName(exeFile))=0 then begin
 h := openprocess(PROCESS_TERMINATE,false,pl[i1].pid);
   TerminateProcess(h,0);
   closehandle(h);
      exit;
    end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
killapp('notepad.exe')
end;