delphi 关闭进程方法

本文介绍如何利用Delphi语言实现关闭指定进程的功能,并通过提升Debug权限来操作服务程序,解决权限不足的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Uses
  Windows,
  SysUtils,
  Tlhelp32 ;

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

Implementation

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: Cardinal ;
Begin
  OpenProcessToken( GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken ) ;
  result := EnablePrivilege( hToken, 'SeDebugPrivilege', True ) ;
  CloseHandle( hToken ) ;
End ;

End.

使用的时候先EnableDebugPrivilege提升权限,然后KillTask( ExeFileName: String )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值