吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2124|回复: 1
收起左侧

[其他转载] 结束进程变函数调用老菜鸟的故事:

[复制链接]
冥界3大法王 发表于 2019-12-8 08:57
本帖最后由 冥界3大法王 于 2019-12-9 13:05 编辑

无费话,说重点!
[Delphi] 纯文本查看 复制代码
procedure TForm1.Button1Click(Sender: TObject);
var
  Form1: TForm1;
implementation
{$R *.dfm}
uses
  Tlhelp32;

const
  PROCESS_TERMINATE = $0001;
var
  ExeFileName: string;
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  ExeFileName := 'x32dbg.exe';
  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
      TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID), 0);
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
end;

这样呢? 成功结束进程了,但是想换另一个进程呢?再粘一遍?用8遍一样的代码?!
  不好不好,还要更进一步,程序员得学会复用代码,马上想到用函数!重复利用才是王道。
[Delphi] 纯文本查看 复制代码
function KillProcess(s: string): string;
const
  PROCESS_TERMINATE = $0001;
var
  ExeFileName: string;
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  //ExeFileName := 'x32dbg.exe';
  ExeFileName := s;
  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
      TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID), 0);
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
end;

然后我们再Button2调用
[Delphi] 纯文本查看 复制代码
procedure TForm1.Button2Click(Sender: TObject);
var
  str1: string;
begin
  str1 := 'x32dbg.exe';
  KillProcess(str1);
end;


这样就做到了最小的开销,重复利用。

免费评分

参与人数 2吾爱币 +3 热心值 +2 收起 理由
iplans + 1 + 1 我很赞同!
苏紫方璇 + 2 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hying95 发表于 2020-2-7 20:13
我爱delphi
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-21 09:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表