Dark_King
asked on
Kill Program ?
How do I kill a Another program from delphi.
Like ctrl+alt+del Do
Like ctrl+alt+del Do
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adjusted points to 40
ASKER
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;
My program has not a visible windows
procedure TForm1.Button1Click(Sender
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 ..
(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(w nd2,@cpid) ;
TerminateProcess(OpenProce ss(PROCESS _TERMINATE ,false,cpi d),1);
end;
procedure TForm1.Button1Click(Sender
var
h : thandle;
begin
h := findwindow('notepad',nil);
edit1.text := inttostr(h);
end;
procedure TForm1.Button2Click(Sender
var
wnd2 : hwnd;
ProcessID,cpid : Cardinal;
begin
wnd2:=strtoint(edit1.text)
ProcessID := GetWindowThreadProcessID(w
TerminateProcess(OpenProce
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(ExtractFileNam e(pl[i1].n ame),Extra ctFileName (exeFile)) =0 then begin
h := openprocess(PROCESS_TERMIN ATE,false, pl[i1].pid );
TerminateProcess(h,0);
closehandle(h);
exit;
end;
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
killapp('notepad.exe')
end;
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(ExtractFileNam
h := openprocess(PROCESS_TERMIN
TerminateProcess(h,0);
closehandle(h);
exit;
end;
end;
procedure TForm1.Button1Click(Sender
begin
killapp('notepad.exe')
end;
MyHandle := FindWindow('notepad', nil);