delphi 关闭进程方法
2012-05-03 16:58
211 查看
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
)
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
)
相关文章推荐
- Delphi 中关闭指定进程的方法
- c#程序强制关闭所有进程方法(转载)
- C#关闭excel进程方法
- ubuntu12.04中OpenSSH守护进程sshd关闭和启动的方法
- C#启动和关闭外部进程的方法
- Delphi中几种关闭方法的介绍和比较(转)
- androd 源码中强制关闭应用程序进程的方法
- c#关闭excel进程失败的解决方法
- 安全彻底关闭实例Excel.Application后产生的进程方法
- linux关闭进程方法
- C#关闭Word进程方法
- sqlserver进程死锁关闭的方法
- 远程关闭进程的方法
- Windows自动更新进程wuauclt.exe 进程服务关闭方法
- Delphi 中表单各种关闭方法的区别
- Ubuntu下关闭进程的方法
- 在Delphi中隐藏程序进程的方法[1]--最简洁的方法
- cidaemon.exe进程cpu占用率高及关闭cidaemon.exe进程方法
- Delphi关闭进程
- Centos下查看占用端口并关闭进程方法