function killsess(const sessname:string):boolean; var p32:TProcessEntry32; hd:thandle; phd:thandle; pname:string; begin result:=false; hd:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if hd=-1 then exit; if not process32first(hd,p32) then begin closehandle(hd); exit; end; repeat pname:=ExtractFileName(p32.szExeFile); if pname=sessname then begin phd:=openprocess(PROCESS_ALL_ACCESS,false,p32.th32ProcessID); TerminateProcess(phd,$ffffffff); break; end; until not process32next(hd,p32); closehandle(hd); result:=true; end;
猛料里有不少这样的代码,贴一个上来. 杀死一个进程 {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;
var
p32:TProcessEntry32;
hd:thandle;
phd:thandle;
pname:string;
begin
result:=false;
hd:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if hd=-1 then
exit;
if not process32first(hd,p32) then
begin
closehandle(hd);
exit;
end;
repeat
pname:=ExtractFileName(p32.szExeFile);
if pname=sessname then
begin
phd:=openprocess(PROCESS_ALL_ACCESS,false,p32.th32ProcessID);
TerminateProcess(phd,$ffffffff);
break;
end;
until not process32next(hd,p32);
closehandle(hd);
result:=true;
end;
杀死一个进程
{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;