腮边淋巴结肿痛怎么办:怎样用VB结束一个进程?

来源:百度文库 编辑:高校问答 时间:2024/05/13 14:22:36
假设任务管理器里面有一个进程名为“notepad.exe”,怎样用VB编个程序来结束它?

' 添加三个CommandButton和一个ListBox

Option Explicit
Dim ProcessID() As Long ' 按list1中的进程顺序存储所有进程ID

'---------- API类型声明 -----------
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 Type

Private Type MODULEENTRY32 '模块
dwsize As Long
th32ModuleID As Long
th32ProcessID As Long
GlblcntUsage As Long
ProccntUsage As Long
modBaseAddr As Byte
modBaseSize As Long
hModule As Long
szModule As String * 256
szExePath As String * 1024
End Type

Private Type THREADENTRY32 '线程
dwsize As Long
cntusage As Long
th32threadID As Long
th32OwnerProcessID As Long
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type

'----------------------------------------- API声明 -------------------------------------------------------
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Module32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long
Private Declare Function Module32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) 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 Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) 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 GetCurrentProcessId Lib "kernel32" () As Long

'---------------------------------------- API常数声明 ------------------------------------------------------
Private Const TH32CS_SNAPHEAPLIST = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE = &H8
Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Private Const TH32CS_INHERIT = &H80000000
Private Const PROCESS_TERMINATE = &H1&

Private Sub Command1_Click()
Dim Process As PROCESSENTRY32
Dim ProcSnap As Long
Dim cntProcess As Long
cntProcess = 0
List1.Clear
ProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If ProcSnap Then
Process.dwsize = 1060 ' 通常用法
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' 遍历所有进程直到返回值为False
List1.AddItem Trim(Process.szExeFile)
cntProcess = cntProcess + 1
Loop
End If
ReDim ProcessID(cntProcess) As Long
Dim i As Long
i = 0
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' 遍历所有进程直到返回值为False
ProcessID(i) = Process.th32ProcessID
i = i + 1
Loop
CloseHandle (ProcSnap)
End Sub

Private Sub Command2_Click()
Dim c As Integer
If List1.ListIndex < 0 Then
MsgBox "请选择进程!", vbOKOnly + vbInformation, "提示"
Else
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID(List1.ListIndex))
If hProcess Then TerminateProcess hProcess, 0
c = List1.ListCount
While List1.ListCount = c
Command1_Click
Wend
End If
End Sub

Private Sub Command3_Click()
Unload Me
End Sub

一、CMD中有结束进程的命令
ntsd -c q -p pid (pid 为进程标识符,在任务管理器中可以调出这一属性列)
例:如explorer.exe的pid为1332,则运行:
ntsd -c q -p 1332就能结束explorer.exe进程
ntsd -c q -pn ***.exe (***.exe为进程名,exe不能省)
例:
运行:ntsd -c q -pn explorer.exe就结束explorer.exe进程
二、应用
用VB作为环境,利用ntsd命令和shell函数就能搞出来了。
思路:
先创建一个批处理文件(直接让CMD接受命令变量感觉不行,而这个文件可以直接在CMD中执行),,预先写入ntsd -c q -p ,然后接受输入的pid,传送PID到BAT文件,点击按钮执行BAT文件。
因为涉及文件操作,所以要在工程中引用microsoft scripting runtime
=========================================================
枫舞添言:如果不明白请,mailto:zhaox_ingg@gmail.com写源代码给你.^_^
hoho~look ..
------------------------------------------------
Dim ts As New FileSystemObject
Dim tf As TextStream
Private Sub Command1_Click()
Set ts = CreateObject("Scripting.FileSystemObject")
Set tf = ts.CreateTextFile("d:\1.bat")
tf.Write ("ntsd -c q -p ") '预先写好前段命令
tf.Write (Text1.Text) '等待写入进程PID
tf.Close
Shell "D:\1.bat", vbMinimizedFocus '最小化执行结束进程命令
Text1.Text = ""
End Sub
Private Sub Form_Unload(Cancel As Integer)
Shell "cmd /c del d:\1.bat", vbMinimizedFocus '关闭时删除临时文件
End Sub

另外一个程序,这个是用输‘进程名’并用‘winexec’来结束进程的代码:
Dim ts As New FileSystemObject
Dim tf As TextStream
Dim df As File
Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long

Private Sub Command1_Click()
Set ts = CreateObject("Scripting.FileSystemObject")
Set tf = ts.CreateTextFile("d:\1.bat")
tf.Write ("ntsd -c q -pn ") '预先写好前段命令
tf.Write (Text1.Text) '等待写入进程PID
tf.Close
WinExec "D:\1.bat", 3 '执行命令
Text1.Text = ""
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set df = ts.GetFile("d:\1.bat")
ts.DeleteFile (df) '关闭时删除临时文件,跟上面方法不一样
End Sub