当前位置:   article > 正文

VB6: 通过窗口句柄得到窗口所在程序的名称和路径(实例)_根据窗口句柄获取窗口名称

根据窗口句柄获取窗口名称
  1. Option Explicit
  2. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  3. Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
  4. Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
  5. Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
  6. Public Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
  7. Public Const PROCESS_QUERY_INFORMATION = &H400
  8. Public Const PROCESS_VM_READ = &H10
  9. Sub main()
  10. If FindWindow(vbNullString, "计算器") = 0 Then
  11. Shell "calc.exe"
  12. End If
  13. Debug.Print GetModuleFileNameByHwnd(FindWindow(vbNullString, "计算器"))
  14. End Sub
  15. '<>
  16. '********************************************************************************
  17. ' 函数: GetModuleFileNameByHwnd
  18. ' 功能: 通过窗口句柄得到模块名称
  19. '********************************************************************************
  20. '<>
  21. Public Function GetModuleFileNameByHwnd(ByVal hWindow As Long) As String
  22. Dim dwProcId As Long
  23. Dim hProcess As Long
  24. Dim hModule As Long
  25. Dim nRet As Long
  26. Dim szBuf As String
  27. Const MAX_SIZE As Long = 256
  28. If hWindow <= 0 Then Exit Function
  29. '' 得到进程ID
  30. Call GetWindowThreadProcessId(hWindow, dwProcId)
  31. If dwProcId = 0 Then Exit Function
  32. '' 根据进程ID,取得进程的句柄
  33. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, dwProcId)
  34. If hProcess = 0 Then Exit Function
  35. '' 枚举进程中的各个模块
  36. nRet = EnumProcessModules(hProcess, hModule, 4&, 0&)
  37. If nRet = 0 Then Exit Function
  38. '' 最后用下面这个函数得到可执行文件的名称
  39. szBuf = String$(MAX_SIZE, vbNullChar)
  40. GetModuleFileNameEx hProcess, hModule, szBuf, Len(szBuf)
  41. GetModuleFileNameByHwnd = StripNulls(szBuf)
  42. End Function
  43. '
  44. '-----------------------------------------------------------------------------
  45. '
  46. '***********************************************************
  47. ' 函数: StripNulls
  48. ' 功能: 清除字符串中多余的Null
  49. '***********************************************************
  50. Public Function StripNulls(ByRef szOriginal As String) As String
  51. Dim i As Long
  52. i = InStr(szOriginal, vbNullChar)
  53. If i > 0 Then
  54. szOriginal = Left$(szOriginal, i - 1)
  55. End If
  56. StripNulls = szOriginal
  57. End Function

参考:     怎样由窗口句柄得到窗口所在程序的名称和路径

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/125215
推荐阅读
相关标签
  

闽ICP备14008679号