whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

38 Visitors Online


 
...enumerate processes and terminate them?
Autor: NetHatcher
[ Print tip ]  

Tip Rating (23):  
     


{
  With the following routines it ist simply easy to kill a running process.
  First build a form with a TListview with 3 columns and a TButton
  to refresh the running processes.
  Attach the Refreshclick-procedure to the TButton and the
  ListViewDblClick-procedure with the TListview
  The TListview shows the processes.
  With a Doubleclick on one of the processnames you can kill this running process.
  Don't forget to include TLHelp32 into your uses-clause!

  Mit der nachfolgend aufgeführten Routinen können Sie die in einer
  Windowssitzung laufenden Prozesse aufzeigen und bei Bedarf auch
  entfernen. Hierfür benötigen Sie ein Formobject, ein ListViewobject und zu-
  mindest ein ButtonObject. Verknüpfen Sie das Buttonobject mit dem BtnRefreshClick
  damit gleich beim Start des Programms alle Prozesse angezeigt werden.
  Zum löschen eines Prozesses müssen Sie eine Verknüpfung zwischen DblClick
  des Listviewobject mit der Procedure ListviewDblClick.
  Wie aus den beigefügten Routinen ersichtlich, kann auch ein einzelner Prozess
  gesucht und terminiert werden. Die hierzu erforderlichen Schritte können aus
  der Refreshroutine entnommen werden.
  Wichtig ist die Einbindung der Unit TlHelp32 !
}

interface

uses
 
{...,}TLHelp32 {important !}

// Global Variables, Globale Variablen

var
  
aSnapshotHandle: THandle;
  aProcessEntry32: TProcessEntry32;
  
implementation

procedure 
TForm1.BtnRefreshClick(Sender: TObject);
var
  
i: Integer;
  bContinue: BOOL;
  NewItem: TListItem;
begin
  
ListView1.Items.Clear;
  aSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  aProcessEntry32.dwSize := SizeOf(aProcessEntry32);
  bContinue := Process32First(aSnapshotHandle, aProcessEntry32);
  while Integer(bContinue) <> 0 do
  begin
    
NewItem := ListView1.Items.Add;
    NewItem.Caption := ExtractFileName(aProcessEntry32.szExeFile);
    NewItem.subItems.Add(IntToHex(aProcessEntry32.th32ProcessID, 4));
    NewItem.subItems.Add(aProcessEntry32.szExeFile);
    bContinue := Process32Next(aSnapshotHandle, aProcessEntry32);
  end;
  CloseHandle(aSnapshotHandle);
end;


procedure TForm1.ListView1DblClick(Sender: TObject);
var
  
Ret: BOOL;
  PrID: Integer; // processidentifier
  
Ph: THandle;   // processhandle
begin
  with 
ListView1 do
  begin
    if 
MessageDlg('Do you want to Terminate "' + ItemFocused.Caption + '"?' + ^J +
                  'It''s possible the system becames instable or out of' + ^J +
                  'control......',
        mtConfirmation, [mbYes, mbNo], 0) = mrYes then
     begin
       
PrID := StrToInt('$' + ItemFocused.SubItems[0]);
       Ph := OpenProcess(1, BOOL(0), PrID);
       Ret := TerminateProcess(Ph, 0);
       if Integer(Ret) = 0 then
         
MessageDlg('Cannot terminate "' + ItemFocused.Caption + '"',
                     mtInformation, [mbOK], 0)
       else
         
ItemFocused.Delete;
     end;
   end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  
{
    ListView1.Columns.Add;
    ListView1.Columns.Add;
    ListView1.Columns.Add;
    ListView1.ViewStyle := vsReport;
  }
  
BtnRefresh.Click;
end;

{Only Win95/Win98/ME}


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners