模块:
'----------------------By 陈锐------------------------------
'如果你要在Internet或BBS上转贴文章,请通知我知道
'Email: blackcat@nease.net develope@163.net
'请参观我的站点 http://www.nease.net/~blackcat
'
'这个程序演示怎样获得当前运行的所有窗口、模块、程序的列表
'
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'该函数是EnumWindows的回调函数,EnumWindows函数将遍历的窗口句柄传递到hwnd参数中
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim astr As String * 256
Dim l As Long
l = GetWindowText(hwnd, astr, Len(astr)) '得到窗口的标题
If InStr(astr, " ") > 1 Then
Form1.List1.AddItem astr
End If
EnumWindowsProc = True
End Function
窗体:
'----------------------By 陈锐------------------------------
'如果你要在Internet或BBS上转贴文章,请通知我知道
'Email: blackcat@nease.net develope@163.net
'请参观我的站点 http://www.nease.net/~blackcat
'
'这个程序演示怎样获得当前运行的所有窗口、模块、程序的列表
'
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) 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 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 EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
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
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
Private Sub CModule_Click()
Dim my As PROCESSENTRY32
Dim l As Long
Dim mm As MODULEENTRY32
Dim lm As Long
List1.Clear
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l > 0 Then
my.dwSize = Len(my)
If Process32First(l, my) Then
Do
'获得进程的标识符
lm = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, my.th32ProcessID)
If lm > 0 Then
mm.dwSize = Len(mm)
If Module32First(lm, mm) Then
Do
If my.th32ProcessID = mm.th32ProcessID Then
List1.AddItem Trim(mm.szExePath) + " " + Str$(mm.modBaseSize)
End If
Loop Until (Module32Next(lm, mm) < 1)
End If
CloseHandle (lm)
End If
Loop Until (Process32Next(l, my) < 1)
End If
CloseHandle (l)
End If
End Sub
Private Sub CProcess_Click()
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
List1.Clear
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
If (Process32First(l, my)) Then '遍历第一个进程
Do
List1.AddItem (Trim(my.szExeFile))
Loop Until (Process32Next(l, my) < 1) '遍历所有进程知道返回值为False
End If
l1 = CloseHandle(l)
End If
End Sub
Private Sub CWindow_Click()
Dim l As Long
List1.Clear
'遍历所有的窗口
l = EnumWindows(AddressOf EnumWindowsProc, 0)
End Sub