|
模块名称:列出指定进程下所有窗体
'无法直接用CreateToolhelp32Snapshot列出指定进程的线程 -CSDN博客
'https://blog.csdn.net/xiaoyao961/article/details/143866893?
- Public Const SMTO_NORMAL = &H0
- Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
- Public 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 * 260
- End Type
- Public Const TH32CS_SNAPTHREAD = &H4
- Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
- Private Declare Function Thread32First Lib "kernel32" ( _
- ByVal hSnapshot As Long, _
- ByRef lpte As THREADENTRY32) As Long
- Private Declare Function Thread32Next Lib "kernel32" ( _
- ByVal hSnapshot As Long, _
- ByRef lpte As THREADENTRY32) As Long
- Private Type THREADENTRY32
- dwSize As Long
- cntUsage As Long
- th32ThreadID As Long
- th32OwnerProcessID As Long
- tpBasePri As Long
- tpDeltaPri As Long
- dwFlags As Long
- End Type
- Public Function GetThreadCount(ByVal PID As Long) As Long
- 'codeid=751 /列出指定进程所有线程数量
- Dim hSnapshot As Long 'dwProcessID is pid
- Dim te32 As THREADENTRY32
- '列出所有进程的线程,然后从中判断
- hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)
- If hSnapshot <> 0 Then
- te32.dwSize = Len(te32)
- Dim ThreadCount As Long
- If Thread32First(hSnapshot, te32) Then
- Do
-
- If te32.th32OwnerProcessID = PID Then
- ThreadCount = ThreadCount + 1
- End If
- Loop While Thread32Next(hSnapshot, te32)
- End If
- Debug.Print "ThreadCount=" & ThreadCount
- CloseHandle (hSnapshot)
- End If
- GetThreadCount = ThreadCount
- End Function
- Public Function ListWindowsByProcessID(ByVal PID As Long) As Long
- 'codeid=752 /列出指定进程下所有窗体
- 'NeedVisible=true 'putlic '是否只列出可见的窗体
- Dim hSnapshot As Long 'dwProcessID is pid
- Dim te32 As THREADENTRY32
- FormCount = 0
- ReDim FormSz(0)
- '列出所有进程的线程,然后从中判断
- hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)
- If hSnapshot <> 0 Then
- te32.dwSize = Len(te32)
- Dim ThreadCount As Long
- If Thread32First(hSnapshot, te32) Then
- Do
-
- If te32.th32OwnerProcessID = PID Then
- ThreadCount = ThreadCount + 1
- 'Debug.Print "线程ID=" & ThreadCount & ",Tid=" & te32.th32ThreadID
- Call EnumThreadWindows(te32.th32ThreadID, AddressOf EnumThreadWndProc, te32.th32ThreadID)
-
- End If
- Loop While Thread32Next(hSnapshot, te32)
- End If
- Debug.Print "ThreadCount=" & ThreadCount
- CloseHandle (hSnapshot)
- End If
- ListWindowsByProcessID = FormCount
- End Function
- Function IsWindowBusy(hwnd As Long, Optional Timeout As Long = 60&) As Boolean
- If SendMessageTimeout(hwnd, WM_GETTEXTLENGTH, 0&, 0&, SMTO_NORMAL, Timeout, ByVal 0&) = 0 Then
- IsWindowBusy = True
- 'DoEvents
- End If
- End Function
复制代码
|
|