kill a task
Author:
Misha Moellner
{For
Windows 9x/ME/2000/XP }
uses
Tlhelp32;
function
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;
procedure TForm1.Button1Click(Sender:
TObject);
begin
KillTask('notepad.exe');
end;
{ For
Windows NT/2000/XP }
procedure
KillProcess(hWindowHandle: HWND);
var
hprocessID: INTEGER;
processHandle: THandle;
DWResult: DWORD;
begin
SendMessageTimeout(hWindowHandle, WM_CLOSE,
0, 0,
SMTO_ABORTIFHUNG or SMTO_NORMAL, 5000,
DWResult);
if isWindow(hWindowHandle) then
begin
// PostMessage(hWindowHandle, WM_QUIT, 0,
0);
{ Get the process identifier for the
window}
GetWindowThreadProcessID(hWindowHandle,
@hprocessID);
if hprocessID <> 0 then
begin
{ Get the process handle }
processHandle :=
OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION,
False, hprocessID);
if processHandle <> 0 then
begin
{ Terminate the process }
TerminateProcess(processHandle, 0);
CloseHandle(ProcessHandle);
end;
end;
end;
end;
procedure
TForm1.Button2Click(Sender: TObject);
begin
KillProcess(FindWindow('notepad',nil));
end;
Inne ródło:
List
processes and kill ...
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants,
Classes, Graphics, Controls, Forms,
Dialogs,TLHelp32, ButtonComps, StdCtrls,
ComCtrls;
type
TForm1 = class(TForm)
BtnRefresh: TAOLButton;
ListView1: TListView;
Label1: TLabel;
procedure BtnRefreshClick(Sender:
TObject);
procedure ListView1DblClick(Sender:
TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
aSnapshotHandle: THandle;
aProcessEntry32: TProcessEntry32;
implementation
{$R *.dfm}
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;
end.
=======Inne
zrodlo
uses
TLHelp32
var
hSnapshot : THandle;
ProcessEntry : TProcessEntry32;
done : BOOL;
begin
try
hSnapshot := CreateToolhelp32Snapshot(
TH32CS_SNAPPROCESS, 0 );
ProcessEntry.dwSize := sizeof(
TProcessEntry32 );
done := Process32First( hSnapshot,
ProcessEntry );
while ( done = true ) do begin
ListBox1.Items.Add((ProcessEntry.szExeFile));
done := Process32Next( hSnapshot,
ProcessEntry );
end;
finally
CloseHandle( hSnapshot );
end;
end;