今天是 2026-6-8 星期一 QQ:545684816 TEL:13979373522
首  页 | 汉字编码 | 科普知识 | 竞赛之窗 | 音乐美术 | 婺中电教  
VB6.0遍历所有进程与PID
发表时间:2021-3-4    浏览次数:30
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 lPid As Long
    Dim Proc As PROCESSENTRY32
    Dim hSnapShot As Long
    List1.Clear
    hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPall, 0)
    Proc.dwSize = Len(Proc)
    lPid = ProcessFirst(hSnapShot, Proc)  

   Do While lPid <> 0
       List1.AddItem Proc.szExeFile
       'Debug.Print Proc.th32ProcessID '获取PID
       lPid = ProcessNext(hSnapShot, Proc)
    Loop
    CloseHandle hSnapShot
End Sub  
Copyright© 2006-2026, All rights reserved。
地址:紫阳镇天佑西路8号 站长:汪有万 建站时间:2006-9-1