ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 92|回复: 0

[分享] VB6 VBA列出指定进程下所有窗体,窗口句柄

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-11-18 21:18 | 显示全部楼层 |阅读模式
模块名称:列出指定进程下所有窗体

'无法直接用CreateToolhelp32Snapshot列出指定进程的线程 -CSDN博客
'https://blog.csdn.net/xiaoyao961/article/details/143866893?

  1. Public Const SMTO_NORMAL = &H0

  2. 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



  3. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

  4. Private Type PROCESSENTRY32

  5.     dwSize As Long

  6.     cntUsage As Long

  7.     th32ProcessID As Long

  8.     th32DefaultHeapID As Long

  9.     th32ModuleID As Long

  10.     cntThreads As Long

  11.     th32ParentProcessID As Long

  12.     pcPriClassBase As Long

  13.     dwFlags As Long

  14.     szExeFile As String * 260

  15. End Type

  16. Public Const TH32CS_SNAPTHREAD = &H4



  17. Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long

  18. Private Declare Function Thread32First Lib "kernel32" ( _

  19.                          ByVal hSnapshot As Long, _

  20.                          ByRef lpte As THREADENTRY32) As Long

  21. Private Declare Function Thread32Next Lib "kernel32" ( _

  22.                          ByVal hSnapshot As Long, _

  23.                          ByRef lpte As THREADENTRY32) As Long





  24. Private Type THREADENTRY32

  25.     dwSize              As Long

  26.     cntUsage            As Long

  27.     th32ThreadID        As Long

  28.     th32OwnerProcessID  As Long

  29.     tpBasePri           As Long

  30.     tpDeltaPri          As Long

  31.     dwFlags             As Long

  32. End Type

  33. Public Function GetThreadCount(ByVal PID As Long) As Long

  34. 'codeid=751 /列出指定进程所有线程数量

  35.     Dim hSnapshot As Long 'dwProcessID is pid

  36.     Dim te32 As THREADENTRY32

  37.     '列出所有进程的线程,然后从中判断

  38.     hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)

  39.     If hSnapshot <> 0 Then

  40.         te32.dwSize = Len(te32)

  41.         Dim ThreadCount As Long

  42.         If Thread32First(hSnapshot, te32) Then

  43.             Do



  44.                 If te32.th32OwnerProcessID = PID Then

  45.                         ThreadCount = ThreadCount + 1

  46.                 End If

  47.             Loop While Thread32Next(hSnapshot, te32)

  48.         End If

  49.         Debug.Print "ThreadCount=" & ThreadCount

  50.         CloseHandle (hSnapshot)

  51.     End If

  52.     GetThreadCount = ThreadCount

  53. End Function



  54. Public Function ListWindowsByProcessID(ByVal PID As Long) As Long

  55. 'codeid=752 /列出指定进程下所有窗体

  56.     'NeedVisible=true 'putlic '是否只列出可见的窗体

  57.     Dim hSnapshot As Long 'dwProcessID is pid

  58.     Dim te32 As THREADENTRY32

  59.     FormCount = 0

  60.     ReDim FormSz(0)

  61.     '列出所有进程的线程,然后从中判断

  62.     hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)

  63.     If hSnapshot <> 0 Then

  64.         te32.dwSize = Len(te32)

  65.         Dim ThreadCount As Long

  66.         If Thread32First(hSnapshot, te32) Then

  67.             Do



  68.                 If te32.th32OwnerProcessID = PID Then

  69.                         ThreadCount = ThreadCount + 1

  70.                         'Debug.Print "线程ID=" & ThreadCount & ",Tid=" & te32.th32ThreadID

  71.                         Call EnumThreadWindows(te32.th32ThreadID, AddressOf EnumThreadWndProc, te32.th32ThreadID)

  72.                        

  73.                 End If

  74.             Loop While Thread32Next(hSnapshot, te32)

  75.         End If

  76.         Debug.Print "ThreadCount=" & ThreadCount

  77.         CloseHandle (hSnapshot)

  78.     End If

  79.     ListWindowsByProcessID = FormCount

  80. End Function





  81. Function IsWindowBusy(hwnd As Long, Optional Timeout As Long = 60&) As Boolean

  82.     If SendMessageTimeout(hwnd, WM_GETTEXTLENGTH, 0&, 0&, SMTO_NORMAL, Timeout, ByVal 0&) = 0 Then

  83.         IsWindowBusy = True

  84.         'DoEvents

  85.     End If

  86. End Function

复制代码


您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-21 20:03 , Processed in 0.039041 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表