• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

Kill Program ?

How do I kill a Another program from delphi.

Like ctrl+alt+del Do
0
Dark_King
Asked:
Dark_King
  • 5
  • 2
1 Solution
 
intheCommented:
hi,

var
  MyHandle: THandle;
begin
  MyHandle := FindWindow(nil, 'TheWindow'sCaption');
  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;

Regards Barry
0
 
intheCommented:
MyHandle could also := FindWindow(TopWindowsclassname, nil);

MyHandle := FindWindow('notepad', nil);
 
0
 
Dark_KingAuthor Commented:
Adjusted points to 40
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
Dark_KingAuthor Commented:
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;
0
 
intheCommented:
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 ..
0
 
intheCommented:
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;
0
 
intheCommented:
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;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now