纯合子 杂合子:VB如果监视进程

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/05 11:25:04
如何让VB程序监视进程中的1.exe,和2.exe,而且进行操作:如果1.exe被关闭时则自动关闭2.exe。

这个该怎么写代码???请写清代码!谢谢

在线等啊!

用timer控件
设成你要监视进程表的时间 以下是每隔1秒查看一次进程 如果没有1.exe 自动关闭2.exe

Option Explicit
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 PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Function TerminateProcess Lib "kernel32" (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
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
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 Const MAX_PATH As Integer = 260
Private Const PROCESS_TERMINATE = &H1

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 * MAX_PATH
End Type
Private Sub Form_Load()
Timer1.Interval=1000
End sub
Private Sub Timer1_Timer()

Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename As String
Dim theloop As Long
Dim flag As Long
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
i = 0
While theloop <> 0 '当返回值非零时继续获取下一个进程
exename = proc.szExeFile

If InStr(exename, "1.exe") <> 0 Then
flag=1

End If
If InStr(exename, "2.exe") <> 0 Then
xx=proc.th32ProcessID

End If
i = i + 1
theloop = ProcessNext(snap, proc)
Wend
CloseHandle snap '关闭进程“快照”句柄
If flag = 0 Then
hProgram = OpenProcess(PROCESS_TERMINATE, 0&, xx)
TerminateProcess hProgram, 0
CloseHandle hProcess

End If

看看这里
http://www.xialiba.net/computer/program/VB/200208/program_594.shtml