vb 如何能在list显示当前正在运行的应用程序名称

2025-01-01 09:54:08
推荐回答(3个)
回答1:

给你个代码

'此模块为监控进程的模块

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

Private Type MODULEENTRY32
dwSize As Long
th32ModuleID As Long ' This module
th32ProcessID As Long ' owning process
GlblcntUsage As Long ' Global usage count on the module
ProccntUsage As Long ' Module usage count in th32ProcessID's context
modBaseAddr As Long ' Base address of module in th32ProcessID's context
modBaseSize As Long ' Size in bytes of module starting at modBaseAddr
hModule As Long ' The hModule of this module in th32ProcessID's context
szModule As String * 256
szExePath As String * 260
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 GetTaskList() '获取系统进程

Dim strMessage As String
Dim I As Long, lPid As Long
Dim Proc As PROCESSENTRY32
Dim hSnapShot As Long
Dim strname As String

Dim HaveProc As Boolean
HaveProc = False

hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
Proc.dwSize = Len(Proc)
lPid = ProcessFirst(hSnapShot, Proc) '获取第一个进程的PROCESSENTRY32结构信息数据

I = 0

Do While lPid <> 0 '当返回值非零时继续获取下一个进程
strname = Proc.szExeFile

strname = Left(strname, InStr(strname, Chr(0)) - 1)
List1.AddItem strname '这句就是添加到listbox
I = I + 1

lPid = ProcessNext(hSnapShot, Proc) '循环获取下一个进程的PROCESSENTRY32结构信息数据
Loop

CloseHandle hSnapShot '关闭进程“快照”句柄

End Sub

'调用方法

Private Sub Form_Load()
GetTaskList
End Sub

回答2:

还真不是很明白你说的什么意思,可能是我学的还不深刻吧。想来学习一下,你不知道的话是不是可以试试SHELL函数呢?

回答3:

用WinAPI

'Option Explicit

Const 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 = &H80000000
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
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

Private Sub Form_Load()
Dim list1 As ListBox
Set list1 = Controls.Add("vb.listbox", "list1", Me)
list1.Visible = True
Me.Move 500, 500, 5000, 6000
Me.Caption = "程序进程"
list1.Move 500, 200, 4000, 4000
Dim hSnapShot As Long, uProcess As PROCESSENTRY32
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
uProcess.dwSize = Len(uProcess)
r = Process32First(hSnapShot, uProcess)
Do While r
list1.AddItem Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0))
'Retrieve information about the next process recorded in our system snapshot
r = Process32Next(hSnapShot, uProcess)
Loop
End Sub