⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 DELPHI 写的结束进程代码,可以查询进程是否在运行 ,也可以强制结束进程式
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,TlHelp32, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Timer1: TTimer;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function CheckTask(ExeFileName: string): Boolean; 
const 
PROCESS_TERMINATE=$0001; 
var 
ContinueLoop: BOOL; 
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin 
result := False; 
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 := True; 
      ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32); 
end; 
end; 


function KillTask(ExeFileName:string):integer; 
const  
PROCESS_TERMINATE = $0001;  
var 
ContinueLoop: BOOLean; 
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 
 if CheckTask(Edit1.Text)=true then
KillTask(Edit1.Text)
else
Label1.Caption:='进程不存在,监视中...';
end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
if CheckTask(Edit1.Text)=true then
Label1.Caption:='进程正在运行中...'
else
Label1.Caption:='进程不存在,监视中...';
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -