VB怎么用TerminateProcess关闭进程?

 我来答
瀟挂
2013-12-11 · TA获得超过103个赞
知道答主
回答量:131
采纳率:0%
帮助的人:161万
展开全部
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本身功能不高。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式