如何用vb监视一个进程是否存在
如何用vb监视一个进程是否存在, 比如: iexplore.exe 存在是杀掉进程,如果不存在就继续监视谁帮我用这代码改写一下
Private Const PROCESS_TERMINATE = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPmodule = &H8
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
Private Type PROCESSENTRY
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 260
End Type
Public Enum PROCESSACTION
paCheckExist = 0
paClose = 1
End Enum
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
'自定义函数GetProc
'strProcessName 为进程名,例如:Explorer.exe
'Action 是一个我自己定义的类型,paClose代表关闭进程;paCheckExist代表检测进程是否存在
Public Function GetProc(ByVal strProcessName As String, Optional ByVal Action As PROCESSACTION = paClose) As Long
Dim hProc As Long, Proc As PROCESSENTRY
Dim hSnap As Long, ExeName As String
Dim lngHwndProcess As Long
hSnap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0&)
With Proc
.dwSize = Len(Proc)
hProc = ProcessFirst(hSnap, Proc)
Do While hProc
ExeName = Left(.szExeFile, InStr(1, .szExeFile, Chr(0)) - 1)
If LCase(ExeName) = LCase(strProcessName) Then
If Action = paClose Then
lngHwndProcess = OpenProcess(PROCESS_TERMINATE, True, .th32ProcessID)
Call TerminateProcess(lngHwndProcess, 0)
Call CloseHandle(lngHwndProcess)
Else
GetProc = True
End If
Exit Do
End If
.szExeFile = String(260, Chr$(0))
hProc = ProcessNext(hSnap, Proc)
Loop
End With
Call CloseHandle(hSnap)
End Function 很简单,
调用Tasklist命令查询,察看得到的结果里面是否有你想查询的进程。 哥们详细一点
Option Explicit
Private Declare Function WinStationTerminateProcess Lib "winsta.dll" (ByVal hServer As Long, ByVal ProcessID As Long, ByVal ExitCode As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
'WinStationTerminateProcess杀进程,第一和第三个参数可设为0
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * 260
End Type
Private Type MyProcess
ExeName As String
pid As Long
End Type
Public Sub CloseProcess(ByVal ProName As String) '此函数用于结束进程,ProName参数为要结束的进程名称
WinStationTerminateProcess 0, FindPid(ProName), 0
End Sub
Public Function IsProcess(ByVal ImName As String) As Boolean '此函数判断一个进程是否存在
Dim ProArr() As String, PIDArr() As Long, i As Integer
ListProcess ProArr, PIDArr
For i = 1 To UBound(ProArr)
If ProArr(i) = ImName Then
IsProcess = True
Exit Function
End If
Next
IsProcess = False
End Function
Private Function FindPid(ByVal ProName As String) As Long
Dim ProArr() As String, PIDArr() As Long, i As Integer
ListProcess ProArr, PIDArr
For i = 1 To UBound(ProArr)
If ProArr(i) = ProName Then
FindPid = PIDArr(i)
Exit For
End If
Next
End Function
Private Sub ListProcess(ByRef ProExeName() As String, ByRef ProPid() As Long)
Dim MyProcess As PROCESSENTRY32
Dim mySnapshot As Long
Dim ProData() As MyProcess
Dim i As Long
ReDim ProData(0)
MyProcess.dwSize = Len(MyProcess)
mySnapshot = CreateToolhelpSnapshot(2, 0)
ProcessFirst mySnapshot, MyProcess
ReDim Preserve ProData(UBound(ProData) + 1)
ProData(UBound(ProData)).ExeName = Left(MyProcess.szexeFile, InStr(MyProcess.szexeFile, Chr(0)) - 1)
ProData(UBound(ProData)).pid = MyProcess.th32ProcessID
While ProcessNext(mySnapshot, MyProcess)
ReDim Preserve ProData(UBound(ProData) + 1)
ProData(UBound(ProData)).ExeName = Left(MyProcess.szexeFile, InStr(MyProcess.szexeFile, Chr(0)) - 1)
ProData(UBound(ProData)).pid = MyProcess.th32ProcessID
Wend
ReDim ProExeName(1 To UBound(ProData))
ReDim ProPid(1 To UBound(ProData))
For i = 1 To UBound(ProData)
With ProData(i)
ProExeName(i) = .ExeName
ProPid(i) = .pid
End With
Next
End Sub
要监视一个进程是否存在,定时调用IsProcess函数就可以了,如果返回true就是存在 结束进程可以直接用taskkill命令
也可以用CloseProcess里的
API WinStationTerminateProcess 没反映 Tasklist可以查询到任务列表,然后看看这个列表中有没有你要的进程。
补充:VB , API