unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, TlHelp32;
//注意手动引用单元TlHelp32
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function KillTask(ExeFileName: string): integer;
const
PROCESS_TERMINATE=$0001;
//进程的PROCESS_TERMINATE访问权限
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //获取系统所有进程快照
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
//调用Process32First前用Sizeof(FProcessEntry32)填充FProcessEntry32.dwSize
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
//获取快照中第一个进程信息并保存到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;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
KillTask('QQ.exe');
end;
end.
[
本帖最后由 shuang200911 于 2009-11-8 14:16 编辑 ]