delphi 关闭进程方法


本文整理自网络,侵删。

 //20200224 更新
Function KillTask( ExeFileName: String ): Integer ; //关闭进程 
Function EnableDebugPrivilege: Boolean ; //提升权限 
Function FindProcessId( ExeFileName: String ): THandle ; //查找进程 

 

unit Unit1;

 

interface

 

uses

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,

  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;

 

type

  TForm1 = class(TForm)

    Timer1: TTimer;

    procedure Timer1Timer(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

 

var

  Form1: TForm1;

 

implementation

 

{$R *.dfm}

uses

 

  Tlhelp32 ;

 

 

Function FindProcessId( ExeFileName: String ): THandle ;

Var

  ContinueLoop: BOOL ;

  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( FProcessEntry32.szExeFile ) = UpperCase( ExeFileName ) Then

    Begin

      result := FProcessEntry32.th32ProcessID ;

      break ;

    End ;

    ContinueLoop := Process32Next( FSnapshotHandle, FProcessEntry32 ) ;

  End ;

  CloseHandle( FSnapshotHandle ) ;

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 ;

 

//但是对于服务程序,它会提示"拒绝访问".其实只要程序拥有Debug权限即可:

 

Function EnableDebugPrivilege: Boolean ;

 

Function EnablePrivilege( hToken: Cardinal ;PrivName: String ;bEnable: Boolean ): Boolean ;

  Var

    TP: TOKEN_PRIVILEGES ;

    Dummy: Cardinal ;

  Begin

    TP.PrivilegeCount := 1 ;

    LookupPrivilegeValue( Nil, pchar( PrivName ), TP.Privileges[ 0 ].Luid ) ;

    If bEnable Then

      TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED

    Else

      TP.Privileges[ 0 ].Attributes := 0 ;

    AdjustTokenPrivileges( hToken, False, TP, SizeOf( TP ), Nil, Dummy ) ;

    Result := GetLastError = ERROR_SUCCESS ;

  End ;

Var

  hToken: THandle ;

Begin

{ 获取进程令牌句柄,设置权限 }

OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,hToken);

  result := EnablePrivilege( hToken, 'SeDebugPrivilege', True ) ;

  CloseHandle( hToken ) ;

End ;

 

 

 

procedure TForm1.Timer1Timer(Sender: TObject);

begin

//使用的时候先提升权限

EnableDebugPrivilege;

KillTask('X32dbG.exe');

KillTask('x64dbg.exe');

KillTask('OLLYDBG.exe');

 

end;

 

end.

相关阅读 >>

Delphi xe 安卓 memo设置字体颜色

Delphi监控指定进程防止被关闭

Delphi 端口扫描器原理

Delphi 与360

Delphi filestream

Delphi 检测服务器地址是否有效

Delphi tfont类型和json互相转换的函数

Delphi extractfileext() 获取文件后缀的函数

Delphi android实例-trectangle加载图片(xe8+小米2)

Delphi编程之系统oem diy

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



打赏

取消

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

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

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

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

评论

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