呵呵,我给你一个示例程序.假设调用记事本程序,并在10秒钟后关闭它:源代码:Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As LongDim ProcessId As Long'调用记事本并在10秒钟后关闭它 Private Sub Command1_Click() ProcessId = 0 ProcessId = Shell("notepad.exe", vbNormalFocus) Timer1.Enabled = True End SubPrivate Sub Form_Load() Timer1.Interval = 10000 Timer1.Enabled = False End Sub'计时: Private Sub Timer1_Timer() If ProcessId <> 0 Then Call KillProcess(ProcessId) '结束调用的记事本 Timer1.Enabled = False End If End Sub'根据进程ID关闭进程过程: Public Sub KillProcess(ByVal PID As Long) On Error GoTo Z Dim hPro As Long Dim ExitCode As Long If PID > 0 Then hPro = OpenProcess(&H1F0FFF, 0, PID) ExitCode = GetExitCodeProcess(hPro, 0) If ExitCode <> 0 Then TerminateProcess hPro, ExitCode End If End If Exit Sub Z: End Sub
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As LongDim ProcessId As Long'调用记事本并在10秒钟后关闭它
Private Sub Command1_Click()
ProcessId = 0
ProcessId = Shell("notepad.exe", vbNormalFocus)
Timer1.Enabled = True
End SubPrivate Sub Form_Load()
Timer1.Interval = 10000
Timer1.Enabled = False
End Sub'计时:
Private Sub Timer1_Timer()
If ProcessId <> 0 Then
Call KillProcess(ProcessId) '结束调用的记事本
Timer1.Enabled = False
End If
End Sub'根据进程ID关闭进程过程:
Public Sub KillProcess(ByVal PID As Long)
On Error GoTo Z
Dim hPro As Long
Dim ExitCode As Long
If PID > 0 Then
hPro = OpenProcess(&H1F0FFF, 0, PID)
ExitCode = GetExitCodeProcess(hPro, 0)
If ExitCode <> 0 Then
TerminateProcess hPro, ExitCode
End If
End If
Exit Sub
Z:
End Sub
在窗体上放一个Timer和一个Command即可
http://www.mndsoft.com/downfiles/EliteSpy.rar
你可以看看 这个程序是如何遍历并结束进程的。