Delphi 中关闭指定进程的方法
2013-12-06 14:44
441 查看
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.
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.
相关文章推荐
- DELPHI实现关闭指定进程,自身防杀
- C#关闭指定名字进程的方法
- Delphi2010 关闭指定的程序进程。
- 关闭指定进程的方法
- delphi 关闭进程方法
- TASKKILL关闭指定进程
- linux关闭进程方法大全【转】
- Android平台中进程与线程以及关闭进程的一些方法
- 【Android】【转】Android程序退出彻底关闭进程的方法
- C#在Web项目中关闭Excel进程的方法
- Windows的cmd命令查询指定端口占用的进程并关闭
- Delphi CreateProcess WIN32API函数CreateProcess用来创建一个新的进程和它的主线程,这个新进程运行指定的可执行文件
- Android程序退出彻底关闭进程的方法
- Inno Setup安装时不能关闭指定进程
- 判断指定进程是否为x64的方法(在ntdll判断某个x64函数是否存在)
- C#关闭excel进程的最有效方法
- Linux下tomcat作为守护进程运行(开机启动、以指定的用户运行、解决非root身份不能绑定1024以下端口的问题)的配置方法
- delphi 对抗任务管理器关闭(提升进程到Debug模式,然后设置进程信息SE_PROC_INFO)
- 在windows系统的cmd窗口中命令查询指定端口占用的进程并关闭
- Delphi结束指定进程EndProcess