以是否运行word为例 Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _ ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As LongPrivate Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, _ ByVal uExitCode As Long) As Long 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 * 1024 End TypeConst TH32CS_SNAPHEAPLIST = &H1 Const TH32CS_SNAPPROCESS = &H2 Const TH32CS_SNAPTHREAD = &H4 Const TH32CS_SNAPMODULE = &H8 Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE) Const TH32CS_INHERIT = &H80000000Dim pid As Long Dim pname As StringPrivate Sub Command1_Click() Dim my As PROCESSENTRY32 Dim l As Long Dim l1 As Long Dim flag As Boolean Dim mName As String Dim i As Integer l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) If l Then my.dwSize = 1060 If (Process32First(l, my)) Then '遍历第一个进程 Do i = InStr(1, my.szExeFile, Chr(0)) mName = LCase(Left(my.szExeFile, i - 1)) If mName = "winword.exe" Then pid = my.th32ProcessID pname = mName MsgBox "找到word" If MsgBox("你想删除 " & mName & " 进程?", vbYesNo + vbQuestion) <> vbYes Then Exit Sub End If
Dim mProcID As Long mProcID = OpenProcess(1&, -1&, pid) TerminateProcess mProcID, 0&
flag = True Exit Sub Else flag = False End If Loop Until (Process32Next(l, my) < 1) '遍历所有进程知道返回值为False End If l1 = CloseHandle(l) End If
If flag = False Then MsgBox "没有找到word" Shell "c:\windows\notepad.exe", vbNormalFocus End If End Sub
学习。。 to online(龙卷风V2.0):能不能在引用API函数时作一下注释,我初学,看不懂。。谢谢!!
实现原理: 首先通过CreateToolhelp32Snapshot函数创建一个进程的快照,然后通过调用Process32First使用快照返回的句柄对进程进行遍历,相关的信息存放在PROCESSENTRY32结构类型的实例中,从而获取相应的线程的ID等的信息。 Private Type PROCESSENTRY32 定义为保存进程相关内容的结构变量 If (Process32First(l, my)) Then '遍历第一个进程将当前进程内容写入进程结构的变量中,并判断是否成功 //通过OpenProcess函数打开当前指定的ID号的进程 mProcID = OpenProcess(1&, -1&, pid)终止进程函数: TerminateProcess()
我明白你的意思下面是一个启动记事本并且监视它的生存状态,如果它在120秒内没有被关闭则强行终止程序,需要一个时钟控件,间隔为1秒, 代码如下 Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode 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 TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Const Process_Query_Information = &H400 Private Const Still_Active = &H103 Dim hProcess As Long, hProcess1 As Long Dim Counter As Integer Dim strs() As String Private Sub Form_Load() Dim cmd As String Dim i As Integer cmd = "c:\windows\notepad.exe" PID = Shell(cmd) Counter = 0 hProcess = OpenProcess(Process_Query_Information, False, PID) hProcess1 = OpenProcess(1, 0, PID) Timer1.Enabled = True End SubPrivate Sub Timer1_Timer() Counter = Counter + 1 Me.Caption = Counter If Counter >= 120 Then TerminateProcess hProcess1, 0 Timer1.Enabled = False Me.Caption = "terminated" Counter = 0 End If GetExitCodeProcess hProcess, lngexitcode If lngexitcode <> Still_Active Then Me.Caption = "closed" Timer1.Enabled = False End If End Sub这个程序里面进程号是通过shell语句得到的,如果是其他不相干的程序,则需要用楼上几位前辈的程序来得到进程号在监视
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As LongPrivate Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long
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 * 1024
End TypeConst TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000Dim pid As Long
Dim pname As StringPrivate Sub Command1_Click()
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
Dim flag As Boolean
Dim mName As String
Dim i As Integer l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
If (Process32First(l, my)) Then '遍历第一个进程
Do
i = InStr(1, my.szExeFile, Chr(0))
mName = LCase(Left(my.szExeFile, i - 1))
If mName = "winword.exe" Then
pid = my.th32ProcessID
pname = mName
MsgBox "找到word"
If MsgBox("你想删除 " & mName & " 进程?", vbYesNo + vbQuestion) <> vbYes Then
Exit Sub
End If
Dim mProcID As Long
mProcID = OpenProcess(1&, -1&, pid)
TerminateProcess mProcID, 0&
flag = True
Exit Sub
Else
flag = False
End If
Loop Until (Process32Next(l, my) < 1) '遍历所有进程知道返回值为False
End If
l1 = CloseHandle(l)
End If
If flag = False Then
MsgBox "没有找到word"
Shell "c:\windows\notepad.exe", vbNormalFocus
End If
End Sub
to online(龙卷风V2.0):能不能在引用API函数时作一下注释,我初学,看不懂。。谢谢!!
If (Process32First(l, my)) Then '遍历第一个进程将当前进程内容写入进程结构的变量中,并判断是否成功
//通过OpenProcess函数打开当前指定的ID号的进程
mProcID = OpenProcess(1&, -1&, pid)终止进程函数:
TerminateProcess()
代码如下
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode 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 TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const Process_Query_Information = &H400
Private Const Still_Active = &H103
Dim hProcess As Long, hProcess1 As Long
Dim Counter As Integer
Dim strs() As String
Private Sub Form_Load()
Dim cmd As String
Dim i As Integer
cmd = "c:\windows\notepad.exe"
PID = Shell(cmd)
Counter = 0
hProcess = OpenProcess(Process_Query_Information, False, PID)
hProcess1 = OpenProcess(1, 0, PID)
Timer1.Enabled = True
End SubPrivate Sub Timer1_Timer()
Counter = Counter + 1
Me.Caption = Counter
If Counter >= 120 Then
TerminateProcess hProcess1, 0
Timer1.Enabled = False
Me.Caption = "terminated"
Counter = 0
End If
GetExitCodeProcess hProcess, lngexitcode
If lngexitcode <> Still_Active Then
Me.Caption = "closed"
Timer1.Enabled = False
End If
End Sub这个程序里面进程号是通过shell语句得到的,如果是其他不相干的程序,则需要用楼上几位前辈的程序来得到进程号在监视