7,763
社区成员
发帖
与我相关
我的任务
分享
'Kill process by executable procedure name for VS
Friend Function KillProcedure(ByVal sExeName As String) As Boolean
Dim lSnapShot As IntPtr
Dim lNextProcess As IntPtr
Dim tPE As PROCESSENTRY32
On Error GoTo ErrMsg
'Create snapshot
lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If lSnapShot.ToInt32 <> -1 Then
'Length of the structure
tPE.dwSize = Len(tPE)
'Find first process
lNextProcess = Process32First(lSnapShot, tPE)
Do While lNextProcess.ToInt32
'Found specified process
If sExeName = Left$(tPE.szExeFile, Len(sExeName)) And Len(sExeName) > 0 Then
Dim lProcess As IntPtr
Dim lExitCode As Long
'Open process
lProcess = OpenProcess(PROCESS_ALL_ACCESS, False, tPE.th32ProcessID)
'Terminate process
TerminateProcess(lProcess, lExitCode)
'Close Handle
CloseHandle(lProcess)
End If
'Get next process
lNextProcess = Process32Next(lSnapShot, tPE)
Loop
'Close Handle
Call CloseHandle(lSnapShot)
Else
Call MsgBox("Cannot enumerate running process!")
Call MsgBox(Err.Description, MsgBoxStyle.OKOnly + MsgBoxStyle.Information, RTX_MESSAGE_TITLE)
Return False
End If
Return True
Exit Function
ErrMsg:
Call MsgBox(Err.Description, MsgBoxStyle.OKOnly + MsgBoxStyle.Information, RTX_MESSAGE_TITLE)
End Function