VB怎么用TerminateProcess关闭进程?
1个回答
展开全部
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpclassname As String, ByVal lpwindowname As String) As Long '查找窗口句柄
Private Sub Command1_Click()
Dim a As Long
a = OpenProcess(&H1F0FFF, False, 进程ID)
TerminateProcess(a, 0)
CloseHandle aEnd Sub补充: 代码有点多,弄得有点乱了知道PID的话,查找窗口句柄那个可以去了
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long '关闭进程
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long ' 打开进程ID
Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF '最大权限打开进程
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long '获取PID
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long '结束进程ID
Private Sub Command1_Click()
Dim a As Long, b As Long, c As Long
a = FindWindow(vbNullString, "abc.exe")
GetWindowThreadProcessId a, b
c = OpenProcess(&H1F0FFF, False, b)
TerminateProcess c, 0
CloseHandle cEnd Sub重新写了个,跟据你的意思来的,我的自己简化了,省了好多代码,你看不明白。 补充: 思路是这样的,先找窗口取得句柄(findwindow),然后通过句柄取得PID(GetWindowThreadProcessId),然后通过PID打开进程(OpenProcess),然后结束进程(TerminateProcess c),最后关闭进程(CloseHandle)。只要学懂这几个API就好理解了。 追问: 上面那段放在标准模块还是类模块,
Private Sub Command1_Click()
Dim a As Long, b As Long, c As Long
a = FindWindow(vbNullString, "abc.exe")
If a <> 0 Then
GetWindowThreadProcessId a, b
c = OpenProcess(&H1F0FFF, False, b)
CloseHandle cElseMsgBox "程序未打开!", 16, "提示"End IfEnd Sub这样就不会错了,TerminateProcess本身不是很好用,应该不能结束系统进程吧,不然也会出错的。这个API本身功能不高。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询