Kill Program ?

How do I kill a Another program from delphi.

Like ctrl+alt+del Do
LVL 6
Dark_KingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
MyHandle could also := FindWindow(TopWindowsclassname, nil);

MyHandle := FindWindow('notepad', nil);
 
0
Dark_KingAuthor Commented:
Adjusted points to 40
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.