VB获取正在运行的进程列表

 我来答
wqlk1
2008-04-07 · TA获得超过1152个赞
知道小有建树答主
回答量:377
采纳率:0%
帮助的人:351万
展开全部
(先自己添加所需控件 list command)

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
Const MAX_PATH As Integer = 260
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
Const TH32CS_SNAPheaplist = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPthread = &H4
Const TH32CS_SNAPmodule = &H8
Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule

Private Sub Command1_Click()
Dim i As Long, lPid As Long
Dim Proc As PROCESSENTRY32
Dim hSnapShot As Long
ListView1.ListItems.Clear
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPall, 0)
Proc.dwSize = Len(Proc)
lPid = ProcessFirst(hSnapShot, Proc)
i = 0
Do While lPid <> 0
ListView1.ListItems.Add , "a" & i, Hex(Proc.th32ProcessID)
ListView1.ListItems("a" & i).SubItems(1) = Proc.szExeFile
i = i + 1
lPid = ProcessNext(hSnapShot, Proc)
Loop
CloseHandle hSnapShot
End Sub

Private Sub Command2_Click()
Dim lPHand As Long, TMBack As Long
If ListView1.SelectedItem.Text <> "" Then
If MsgBox("确定要结束进程 " & ListView1.SelectedItem.SubItems(1) & " 吗?", vbYesNo) = vbYes Then
lPHand = Val("&H" & ListView1.SelectedItem.Text)
lPHand = OpenProcess(1&, True, lPHand)
TMBack = TerminateProcess(lPHand, 0&)
If TMBack <> 0 Then
MsgBox ListView1.SelectedItem.SubItems(1) & "已终止!"
Else
MsgBox ListView1.SelectedItem.SubItems(1) & "不能终止!"
End If
CloseHandle lPHand
Command1_Click
End If
End If
End Sub

Private Sub form_Load()
Command1.Caption = "刷新"
Command2.Caption = "结束进程"
ListView1.ColumnHeaders.Clear
ListView1.ColumnHeaders.Add , "a", "ID", 600
ListView1.ColumnHeaders.Add , "b", "EXE", 4000
ListView1.View = lvwReport
Command1_Click
End Sub

以上代码仅供参考.
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式