Delphi 中关闭指定进程的方法

  1. Uses    
  2.   Windows,    
  3.   SysUtils,    
  4.   Tlhelp32 ;    
  5.   
  6. Function KillTask( ExeFileName: String ): Integer ; //关闭进程    
  7. Function EnableDebugPrivilege: Boolean ; //提升权限    
  8. Function FindProcessId( ExeFileName: String ): THandle ; //查找进程    
  9.   
  10. Implementation    
  11.   
  12. Function FindProcessId( ExeFileName: String ): THandle ;    
  13. Var    
  14.   ContinueLoop: BOOL ;    
  15.   FSnapshotHandle: THandle ;    
  16.   FProcessEntry32: TProcessEntry32 ;    
  17. Begin    
  18.   result := 0 ;    
  19.   FSnapshotHandle := CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ;    
  20.   FProcessEntry32.dwSize := Sizeof( FProcessEntry32 ) ;    
  21.   ContinueLoop := Process32First( FSnapshotHandle, FProcessEntry32 ) ;    
  22.   While integer( ContinueLoop ) <> 0 Do    
  23.   Begin    
  24.     If UpperCase( FProcessEntry32.szExeFile ) = UpperCase( ExeFileName ) Then    
  25.     Begin    
  26.       result := FProcessEntry32.th32ProcessID ;    
  27.       break ;    
  28.     End ;    
  29.     ContinueLoop := Process32Next( FSnapshotHandle, FProcessEntry32 ) ;    
  30.   End ;    
  31.   CloseHandle( FSnapshotHandle ) ;    
  32. End ;    
  33.   
  34. Function KillTask( ExeFileName: String ): Integer ;    
  35. Const    
  36.   PROCESS_TERMINATE = $0001 ;    
  37. Var    
  38.   ContinueLoop: boolean ;    
  39.   FSnapshotHandle: THandle ;    
  40.   FProcessEntry32: TProcessEntry32 ;    
  41. Begin    
  42.   Result := 0 ;    
  43.   FSnapshotHandle := CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ;    
  44.   FProcessEntry32.dwSize := SizeOf( FProcessEntry32 ) ;    
  45.   ContinueLoop := Process32First( FSnapshotHandle, FProcessEntry32 ) ;    
  46.   
  47.   While Integer( ContinueLoop ) <> 0 Do    
  48.   Begin    
  49.     If ( ( UpperCase( ExtractFileName( FProcessEntry32.szExeFile ) ) =    
  50.       UpperCase( ExeFileName ) ) Or ( UpperCase( FProcessEntry32.szExeFile ) =    
  51.       UpperCase( ExeFileName ) ) ) Then    
  52.       Result := Integer( TerminateProcess(    
  53.         OpenProcess( PROCESS_TERMINATE,    
  54.         BOOL( 0 ),    
  55.         FProcessEntry32.th32ProcessID ),    
  56.         0 ) ) ;    
  57.     ContinueLoop := Process32Next( FSnapshotHandle, FProcessEntry32 ) ;    
  58.   End ;    
  59.   CloseHandle( FSnapshotHandle ) ;    
  60. End ;    
  61.   
  62. //但是对于服务程序,它会提示"拒绝访问".其实只要程序拥有Debug权限即可:    
  63.   
  64. Function EnableDebugPrivilege: Boolean ;    
  65.   
  66. Function EnablePrivilege( hToken: Cardinal ;PrivName: String ;bEnable: Boolean ): Boolean ;    
  67.   Var    
  68.     TP: TOKEN_PRIVILEGES ;    
  69.     Dummy: Cardinal ;    
  70.   Begin    
  71.     TP.PrivilegeCount := 1 ;    
  72.     LookupPrivilegeValue( Nil, pchar( PrivName ), TP.Privileges[ 0 ].Luid ) ;    
  73.     If bEnable Then    
  74.       TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED    
  75.     Else    
  76.       TP.Privileges[ 0 ].Attributes := 0 ;    
  77.     AdjustTokenPrivileges( hToken, False, TP, SizeOf( TP ), Nil, Dummy ) ;    
  78.     Result := GetLastError = ERROR_SUCCESS ;    
  79.   End ;    
  80. Var    
  81.   hToken: Cardinal ;    
  82. Begin    
  83.   OpenProcessToken( GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken ) ;    
  84.   result := EnablePrivilege( hToken, 'SeDebugPrivilege', True ) ;    
  85.   CloseHandle( hToken ) ;    
  86. End ;    
  87.   
  88. End.    
  89.     
原文地址:https://www.cnblogs.com/wxb-km/p/3461390.html