Board index » delphi » TaskManager - Kill Process ~?!~

TaskManager - Kill Process ~?!~

Hi, i am in desprate need of just a very simple but effective task manager.
I have created one that grabs the current prcesses i have a refresh button
and a end process button. The only problem i have is when i hit end process
it doesent always end the process acculy it only ends a few of the
processes. Would someone post upload etc.. a task man that when you hit end
proccess it completly kills the process and whipes it out ? or just post to
code so i can add it into mine. I could really use this asap..
Thank you
DaN
 

Re:TaskManager - Kill Process ~?!~


Below is what iv found in my reasherch, if it compiled id be all set :(

~~~~~~~~~~~~~~~~~~~~~
unit testproc;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Tlhelp32, StdCtrls, ImgList, Menus, ActnList, Buttons, RXCtrls, Placemnt,
  ComCtrls;

type
  TForm1 = class(TForm)
    ActionList1: TActionList;
    PopupMenu1: TPopupMenu;
    RefreshAction: TAction;
    KillAction: TAction;
    RefreshAction1: TMenuItem;
    KillAction1: TMenuItem;
    ImageList1: TImageList;
    GroupBox1: TGroupBox;
    Flagslbl: TLabel;
    ucountlbl: TLabel;
    procidlbl: TLabel;
    heaplbl: TLabel;
    modlbl: TLabel;
    threadlbl: TLabel;
    parentlbl: TLabel;
    priolbl: TLabel;
    Theprio: TLabel;
    TheFlags: TLabel;
    Thecount: TLabel;
    TheProcID: TLabel;
    TheHeapId: TLabel;
    TheModID: TLabel;
    TheThreads: TLabel;
    Theparent: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    Label1: TLabel;
    ListView1: TListView;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure RefreshActionExecute(Sender: TObject);
    procedure KillActionExecute(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure ListBox1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
  private
    procedure UpdateLabels;
    procedure LookForExes;    // update the kist box with the executable
names
    procedure EmptyListbox;
    { Private declarations }
  public
    { Public declarations }
  end;
(***************************************************************************
*****************)
  procObject = class             // class for use with Tlistbox.objects
    private
      fProc : TProcessEntry32;   // declare all data private for any future
changes
      procedure putProcData(const value : TProcessEntry32);
      function  getProcData: TProcessEntry32;
    public
      constructor create(procinfo : TProcessEntry32);
    published
      property Theproc : TProcessEntry32 read getProcData write putProcData;
  end;
(***************************************************************************
*****************)
var
  Form1: TForm1;

implementation

{$R *.DFM}
(***************************************************************************
*****************)
constructor procObject.create(procinfo :TProcessEntry32);
begin
  inherited create;
  theproc := procinfo;
end;
(***************************************************************************
*****************)
procedure procObject.putProcData(const value : TProcessEntry32);
begin
  fproc := value;
end;
(***************************************************************************
*****************)
function procObject.getProcData : TProcessEntry32;
begin
  result := fproc;
end;
(***************************************************************************
*****************)
procedure TForm1.updatelabels;
begin
 if listbox1.itemindex <> -1 then
    with procObject(listbox1.items.objects[listbox1.itemindex]).theproc do
    begin
      theflags.caption   := inttostr(dwFlags);
      thecount.caption   := inttostr(cntUsage);
      theprocid.caption  := inttostr(th32ProcessID);
      theheapid.caption  := inttostr(th32DefaultHeapID);
      themodid.caption   := inttostr(th32DefaultHeapID);
      thethreads.caption := inttostr(cntthreads);
      theparent.caption  := inttostr(th32ParentProcessID);
      case pcPriClassBase of
         4 :  theprio.caption := 'Low priority';          // the only
possible priority values
         8 :  theprio.caption := 'Normal priority';       // are 4, 8, 13,
and 24
        13 :  theprio.caption := 'High priority';
        24 :  theprio.caption := 'Real-time priority';
      end;
    end
end;
(***************************************************************************
*****************)
procedure Tform1.EmptyListbox;
var x: integer;
    allitems : integer;
begin
  allitems :=listbox1.Items.count;
  if allitems > 0 then
     for x:=0 to allitems-1 do
         listbox1.items.objects[x].free
end;
(***************************************************************************
*****************)
procedure Tform1.LookForExes;
var
   Proc   : TProcessEntry32;
   Snap   : THandle;
   prochandle : procObject;
   tempname : string;
   oldindex : integer;
begin
   oldindex := listbox1.ItemIndex;  // save old listbox position
   EmptyListbox;
   try
     listbox1.items.BeginUpdate;    // disable screen refreshes during
update
     listbox1.items.clear;
     try
       Snap := CreateToolHelp32SnapShot(TH32CS_SNAPALL,0); // API calls.
Picked from
       Proc.dwSize := SizeOf(TProcessEntry32);             // someone on the
Borland NG's
       Process32First(Snap,Proc);                          // Sorry ...
forgot the name.
       repeat
         prochandle := procObject.create(proc);
         if length(string(Proc.szExeFile)) > 0 then        // check for
processes without a
            tempname := proc.szExeFile                     // description
         else
            tempname := '< Unknown >';
         listbox1.items.addobject(tempname,prochandle);
       until (not Process32Next(Snap,Proc));
     except
       showmessage('An error occurred while reading task information');
     end;
   finally
     listbox1.items.endupdate;
     if listbox1.Items.Count > 0 then     // set listbox position to old
position - 1
        begin
          if oldindex > 0 then
             listbox1.ItemIndex := oldindex -1
          else
             listbox1.ItemIndex := 0;
        end
     else
        KillAction.Enabled := false;
   end;
end;
(***************************************************************************
*****************)
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  EmptyListbox;
end;
(***************************************************************************
*****************)
procedure TForm1.RefreshActionExecute(Sender: TObject);
begin
  Lookforexes;
  updatelabels;
end;
(***************************************************************************
*****************)
procedure TForm1.KillActionExecute(Sender: TObject);
var proc : procobject;
    ExitCD : integer;
// This does a "hard" kill. From the MS help file :
// The TerminateProcess function is used to unconditionally cause a process
to exit.
// Use it only in extreme circumstances. The state of global data maintained
by dynamic-link
// Libraries (DLLs) may be compromised if TerminateProcess is used rather
than ExitProcess.
// TerminateProcess causes all threads within a process to terminate, and
causes a process
// to exit, but DLLs attached to the process are not notified that the
process is terminating.
begin
 ExitCD := 0;
 if listbox1.itemindex <> -1 then
    begin
      proc := procObject(listbox1.items.objects[listbox1.itemindex]); // get
handle
      try                                                             // and
kill process

TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,Proc.Theproc.th32Proces
sID),
                          ExitCd);
      except
         showmessage('An error has occurred while trying to kill the
selected application.'+#13+
                     'Error code :'+inttostr(ExitCD));
      end;
    end;
  if ExitCD = 0 then
     begin
        screen.Cursor := crhourglass;
        sleep(200);           // wait for windows to kill the task before
refreshing the app list
        lookforexes;          // refresh the process list
        screen.cursor := crDefault;
        updatelabels;         // and display selected process information
     end;
end;
(***************************************************************************
*****************)
procedure TForm1.ListBox1Click(Sender: TObject);
begin
  updatelabels;
end;
(***************************************************************************
*****************)
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
   application.terminate;
end;
(***************************************************************************
*****************)
procedure TForm1.ListBox1KeyPress(Sender: TObject; var Key: Char);
begin
  updatelabels;
end;
(***************************************************************************
*****************)
procedure TForm1.FormCreate(Sender: TObject);
begin
  lookforexes;
  updatelabels;
end;
(***************************************************************************
*****************)
end.

Re:TaskManager - Kill Process ~?!~


If someone could get that to compile id be all set.. :(
all i need is the refresh hooked up to a t timer w/ a process kill button.
(w/ hard kill)

Re:TaskManager - Kill Process ~?!~


hmm i did some more codding and i got a hardkill but now iv lost the list
view and the slection.

help please...

-dAnnnn

Re:TaskManager - Kill Process ~?!~


ok i got my task man to work... it wont kill sys proccesss though, any
ideas?
i found this in my reashearch
http://www.delphi3000.com/articles/article_857.asp
but it dident compile full of errors.
Quote

Other Threads