怎样用VB获取当前系统进程名????? 要求:

要求:在窗体上只有两个控件Command与Listbox,单击command则把当前系统进程名添加到list里面,添加的只是进程名。这个是可以,只是运行后带有文件路径,我... 要求:在窗体上只有两个控件Command与Listbox ,单击command则把当前系统进程名添加到list里面,添加的只是进程名。
这个是可以,只是运行后带有文件路径,我想要实现的是,就像windows任务管理器一样,只是一个纯进程名,
展开
 我来答
ljrlili
2010-11-09 · TA获得超过892个赞
知道小有建树答主
回答量:594
采纳率:50%
帮助的人:319万
展开全部
Option Explicit

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function EnumProcesses Lib "PSAPI.DLL" (lpidProcess As Long, ByVal cb As Long, cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameEx Lib "PSAPI.DLL" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "PSAPI.DLL" (ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, lpcbNeeded As Long) As Long

Const PROCESS_QUERY_INFORMATION = &H400
Const PROCESS_VM_READ = &H10
Const MAX_PATH As Long = 260

Private Sub Command1_Click()
Dim szProcessName As String
Dim aProcesses() As Long
Dim hMod(0 To 1023) As Long
Dim hProcess As Long
Dim lLen As Long
Dim cbNeeded As Long
Dim cProcesses As Long
Dim lProcessID As Long
Dim K
Dim i As Long
ReDim aProcesses(0 To 1023) As Long
K = List1.ListIndex
List1.Clear
If (EnumProcesses(aProcesses(0), 1024 * 4, cbNeeded) <> 0) Then
cProcesses = cbNeeded / 4
For i = 0 To cProcesses - 1
lProcessID = aProcesses(i)
lLen = MAX_PATH
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcessID)
If (lProcessID = 0) Then
List1.AddItem "System Idle Process"
ElseIf lProcessID < 5 Then
List1.AddItem "System"
Else
If (hProcess <> 0) Then
If (EnumProcessModules(hProcess, hMod(0), 1024 * 4, cbNeeded)) Then
szProcessName = String$(lLen, 0)
LSet szProcessName = "unknown"
GetModuleFileNameEx hProcess, hMod(0), szProcessName, lLen
List1.AddItem szProcessName
End If
End If
End If
Next i
If K < 2 Or K > List1.ListCount - 1 Then K = List1.ListCount - 1
List1.ListIndex = K
Else
MsgBox "Failed to get process list.", vbCritical
End If

End Sub
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式