Title: How to enumerate processes and terminate them
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