龙空技术网

VB 强制终止进程

鳞潜羽翔147 167

前言:

今天你们对“vbnet程序退出事件”可能比较讲究,兄弟们都需要知道一些“vbnet程序退出事件”的相关知识。那么小编在网络上汇集了一些对于“vbnet程序退出事件””的相关内容,希望兄弟们能喜欢,咱们快快来了解一下吧!

'强制终止进程 2022.10.25

'说明:FindWindow取得窗口的句柄,用GetWindowThreadProcessid取得进程的procid,以procid为参数用OpenProcess取得进程句柄,用TerminateProcess终止该进程,hprocess为进程句柄,uExitCode为0

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long '检索处理顶级窗口的类名和窗口名称匹配指定的字符串。这个函数不搜索子窗口。

Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long '取得进程的 Procid

Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Public Function ZzJc(CkBt As String, GbLx As Integer) As Long '终止进程,窗口标题,关闭类型,返回值(0关闭失败,1关闭成功,指定窗口不存在)

Dim Hwnd As Long '窗口句柄

Dim Procid As Long '进程ID

Dim ProId As Long

Dim hPro As Long '进程句柄

' CkBt = "模拟器" & Format(Val(Form2.Text10.Text), "#00")

Hwnd = FindWindow(vbNullString, CkBt) '用FindWindow函数查找指定的"模拟器"窗口

Delay 100

If Hwnd = 0 Then

Form2.Text11.Text = "不存在"

ZzJc = 2

Form2.Text14.Text = ZzJc

Exit Function

End If

Form2.Text11.Text = Hwnd

Procid = GetWindowThreadProcessId(Hwnd, ProId) '取该窗口所属的进程ID

Delay 100

Form2.Text12.Text = ProId

hProc = OpenProcess(&H1, True, ProId) '获取进程句柄

Delay 100

Form2.Text13.Text = hProc

ZzJc = TerminateProcess(hProc, 0) '关闭进程

Delay 100

Form2.Text14.Text = ZzJc

End Function

标签: #vbnet程序退出事件