Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

A challenging Question about ending a process

hello
     How do i end a windows process, say if the system runs a process called myfirst.exe and this can be viewed in the Task Manager > Processes, now the problem is i have to end this process and call another, i am struck with this plz help me ..............         Sam
0
samuel_chinnu
Asked:
samuel_chinnu
  • 8
  • 6
  • 4
  • +1
1 Solution
 
geobulCommented:
Hi,

It would be easier if that exe has a window and you know the class name or the caption of that window. Is this your case?

Regards, Geo
0
 
cjm30305Commented:
function TForm1.KillTask(ExeFileName: string): integer;
const
 PROCESS_TERMINATE=$0001;
var
 ContinueLoop: BOOL;
 FSnapshotHandle: THandle;
 FProcessEntry32: TProcessEntry32;
begin
 result := 0;

 FSnapshotHandle := CreateToolhelp32Snapshot
                   (TH32CS_SNAPPROCESS, 0);

 FProcessEntry32.dwSize := Sizeof(FProcessEntry32);

 ContinueLoop := Process32First(FSnapshotHandle,
                                FProcessEntry32);

 while integer(ContinueLoop) <> 0 do
  begin
   if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
        UpperCase(ExeFileName))
    or (UpperCase(FProcessEntry32.szExeFile) =
        UpperCase(ExeFileName)))
     then
      Result := Integer(TerminateProcess(OpenProcess(
                        PROCESS_TERMINATE, BOOL(0),
                        FProcessEntry32.th32ProcessID), 0));
   ContinueLoop := Process32Next(FSnapshotHandle,
                                 FProcessEntry32);
  end;

 CloseHandle(FSnapshotHandle);
end;


Cheers!!!
0
 
cjm30305Commented:
BTW - This will kill ALL processes with that name.  If you are running one session of Notepad.exe or 20 Notepad.exe, all will be gone....

Have fun!!!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
geobulCommented:
cjm30305,

Your code will NOT work under Windows NT.

Regards, Geo
0
 
cjm30305Commented:
I do not see where there is a requirement for NT.  This WILL work on 2000 and XP.  
0
 
cjm30305Commented:
I do not see where there is a requirement for NT.  This WILL work on 2000 and XP.  
0
 
cjm30305Commented:
Also, NT will be desupported in June of this year.  Why would someone be doing new development in OS that is to be desupported?
0
 
geobulCommented:
I prefer posting WM_QUIT message to the main window of the app:

var
  h: HWND;
begin
  h := FindWindow('ClassName', 'Title'); // one of them or both
  if IsWindow(h) then PostMessage(h, WM_CLOSE, 0, 0);
end;

TollHelp32 is not available in NT 4.
 
Another thing is that OpenProcess API creates a handle which has to be freed by calling CloseHandle API.

Regards, Geo
0
 
trident_jobCommented:
From Smurff
-----------------------------------------
procedure KillApp(title : string);
var h : HWND;

begin
h:=FindWindow(nil,@(title)[1]);
if h<>0 then
       begin
       SendMessage(h,WM_CLOSE,0,0);
       Showmessage('App terminated.')
       end
       else
       Showmessage('failed kill app.')
end;

//to close notepad,
KillApp('Notepad');
Hope this helps
0
 
cjm30305Commented:
Okay all, FindWindow will find the specified window by it's title.  You have both provided a solution for a process that has an open window.

However, FindWindow will not kill an app that is listed in the task manager list that does not have an associated window.  And yes, you must use the CloseHandle API, as the above example shows, but this is the only way to kill an app that has no window, but an open process, per the initial requirement from samuel_chinnu.

Cheers.
0
 
cjm30305Commented:
Example, close any windows explorer sessions, and then pass explorer.exe into your function/procedure, then pass it into mine.  Mine will work and kill explorer.exe (you will loose your desktop), however FindWindow reports 'Failed kill app'.
0
 
samuel_chinnuAuthor Commented:
Hi cjm30305
Thanks for ur valuable answer, but i ended up with some errors while compiling , it would be great if u can clear the errors for me waiting for ur relpy........Sam
0
 
samuel_chinnuAuthor Commented:
Hi cjm30305
Thanks for ur valuable answer, but i ended up with some errors while compiling , it would be great if u can clear the errors for me waiting for ur relpy........Sam
0
 
samuel_chinnuAuthor Commented:
Hi cjm30305
Thanks for ur valuable answer, but i ended up with some errors while compiling , it would be great if u can clear the errors for me waiting for ur relpy........Sam
0
 
cjm30305Commented:
What errors did you encounter?  The only thing that I can think of is you may need Tlhelp32 in your uses clause.

If that does not work, please send me the text of the errors you got when compiling.

What version of Delphi are you using?  This bit of code was first written by me in Delphi 5 and has been ported to 6 and 7.

Jake
0
 
geobulCommented:
Hi,

In order to close all opened handles correctly, change:
---
Result := Integer(TerminateProcess(OpenProcess(
                       PROCESS_TERMINATE, BOOL(0),
                       FProcessEntry32.th32ProcessID), 0));
---

to:

---
var hPID: THandle;
...
hPID := OpenProcess(PROCESS_TERMINATE, false,        FProcessEntry32.th32ProcessID);
if hPID <> 0 then begin
  try
    result := Integer(TerminateProcess(hPID, 0));
  finally
    CloseHandle(hPID);
  end;
end;
---
 
Regards, Geo
0
 
samuel_chinnuAuthor Commented:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function TForm1.KillTask(ExeFileName: string): integer;
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result := 0;

FSnapshotHandle := CreateToolhelp32Snapshot
                  (TH32CS_SNAPPROCESS, 0);

FProcessEntry32.dwSize := Sizeof(FProcessEntry32);

ContinueLoop := Process32First(FSnapshotHandle,
                               FProcessEntry32);

while integer(ContinueLoop) <> 0 do
 begin
  if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
       UpperCase(ExeFileName))
   or (UpperCase(FProcessEntry32.szExeFile) =
       UpperCase(ExeFileName)))
    then
     Result := Integer(TerminateProcess(OpenProcess(
                       PROCESS_TERMINATE, BOOL(0),
                       FProcessEntry32.th32ProcessID), 0));
  ContinueLoop := Process32Next(FSnapshotHandle,
                                FProcessEntry32);
 end;

CloseHandle(FSnapshotHandle);
end;

end.


Errors:
[Error] Unit1.pas(23): Undeclared identifier: 'KillTask'
[Error] Unit1.pas(29): Undeclared identifier: 'TProcessEntry32'
[Error] Unit1.pas(31): Undeclared identifier: 'result'
[Error] Unit1.pas(33): Undeclared identifier: 'CreateToolhelp32Snapshot'
[Error] Unit1.pas(34): Undeclared identifier: 'TH32CS_SNAPPROCESS'
[Error] Unit1.pas(36): Missing operator or semicolon
[Error] Unit1.pas(38): Undeclared identifier: 'Process32First'
[Error] Unit1.pas(43): ')' expected but identifier 'szExeFile' found
[Error] Unit1.pas(62): Statement expected but end of file found

Hi cjm30305
the errors i have stated could be silly but im just new to this language and im using delphi 7 waiting for ur reply .....sam
0
 
samuel_chinnuAuthor Commented:
Hi cjm30305
Thanks for ur valuable answer, as u said the error was is didnt add Tlhelp32 in uses clause tx very much.....sam
0
 
samuel_chinnuAuthor Commented:
Hi cjm30305

One more query the codes above works perfect with normal process if its a system process how do i deal it...Sam
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 8
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now