DELPHI Windows 底下根据一个进程的名字杀死一个进程的代码


本文整理自网络,侵删。

 
知道一个正在运行的进程的名字,比如计算器:Calc.exe 
杀死它的代码:

USES  TLHelp32;   //必须 Users 这个,FindProcessID 函数需要用到它。

{$R *.dfm}


function FindProcessID(s:string):integer;
var
  found,find:boolean;
  FSnapshotHandle:tHANDLE;
  lppe:TProcessEntry32;
begin
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //CreateToolhelp32Snapshot函数得到进程快照
  Find:=False;
  lppe.dwSize := Sizeof(lppe); //初始化
  found := Process32First(FSnapshotHandle, lppe); //Process32First 得到一个系统快照里第一个进程的信息
  while found do
      begin
      if LowerCase(ExtractFileName(lppe.szExeFile))=LowerCase(s) then
        begin
        Result:=lppe.th32ProcessID; //找到进程返回ID
        find:=true;
        CloseHandle(FSnapshotHandle);
        exit;
        end;
        found := Process32Next(FSnapshotHandle, lppe);
      end;
  CloseHandle(FSnapshotHandle);
  if find=False then
  Result:=0; //找不到进程返回0
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  ProH: THandle;
  ProcID: DWord;
begin
  ProcID := FindProcessID(Edit1.Text);

  ProH := OpenProcess(PROCESS_ALL_ACCESS, True, ProcID);
  TerminateProcess(ProH, 0);
end;

相关阅读 >>

Delphi 日期时间计算

Delphi 取目录下指定文件到列表

Delphi从字符串中取出数字

Delphi 实现php的urlencode编码效果

Delphi tms web core webhttprequest1 解析json

Delphi xe10 创建匿名线程

Delphi从路径中分离文件名

Delphi在64位系统下写注册表注意事项

Delphi 加载excel 导入数据库

Delphi excel数据导入数据库表

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...