//需要引用tlhelp32单元
//查找进程
function findProcessId(pname:string):Cardinal;
var hsnapshot:THandle;
lpe:TProcessEntry32;
found:Boolean;
begin
Result:=0;
if pname<>'' then
begin
hsnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
try
lpe.dwSize := SizeOf(TProcessEntry32);//在调用Process32First API之前,需要初始化lppe记录的大小
found:=Process32First(hsnapshot,lpe);
while found do
begin
if UpperCase(lpe.szExeFile)=UpperCase(pname) then
begin
Result:=lpe.th32ProcessID;
Break;
end;
found:=Process32Next(hsnapshot,lpe);
end;
finally
//关闭快照句柄
closehandle(hsnapshot);
end;
end;
end;
//杀死进程
function killProcessByid(pid:Cardinal):Boolean;
var hd:THandle;
begin
Result:=False;
if pid<>0 then
begin
hd:=OpenProcess(PROCESS_ALL_ACCESS,true,pid);
try
if hd<>0 then
begin
Result:=TerminateProcess(hd,0);
end;
finally
CloseHandle(hd);
end;
end;
end;