Advertisement
Guest User

PID number

a guest
Mar 21st, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
  4. (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  5. Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" _
  6. (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
  7. Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
  8. (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
  9.  
  10. Private x As Integer
  11.  
  12.  'Used a user defined type here rather than Enum so that it works on 97
  13. Private Type winEnum
  14.     winHandle As Integer
  15.     winClass As Integer
  16.     winTitle As Integer
  17.     winHandleClass As Integer
  18.     winHandleTitle As Integer
  19.     winHandleClassTitle As Integer
  20. End Type
  21. Dim winOutputType As winEnum
  22.  
  23. Public Sub GetWindows()
  24.     x = 0
  25.     winOutputType.winHandle = 0
  26.     winOutputType.winClass = 1
  27.     winOutputType.winTitle = 2
  28.     winOutputType.winHandleClass = 3
  29.     winOutputType.winHandleTitle = 4
  30.     winOutputType.winHandleClassTitle = 5
  31.    
  32.     GetWinInfo 0&, 0, winOutputType.winHandleClassTitle
  33.  
  34. End Sub
  35.  
  36.  
  37. Private Sub GetWinInfo(hParent As Long, intOffset As Integer, OutputType As Integer)
  38.      'Sub to recursively obtain window handles, classes and text
  39.     'given a parent window to search
  40.     'Written by Mark Rowlinson
  41.     'www.markrowlinson.co.uk - The Programming Emporium
  42.    Dim hwnd As Long, lngRet As Long, y As Integer
  43.     Dim strText As String
  44.     hwnd = FindWindowEx(hParent, 0&, vbNullString, vbNullString)
  45.     While hwnd <> 0
  46.         Select Case OutputType
  47.         Case winOutputType.winClass
  48.             strText = String$(100, Chr$(0))
  49.             lngRet = GetClassName(hwnd, strText, 100)
  50.             Range("a1").Offset(x, intOffset) = Left$(strText, lngRet)
  51.         Case winOutputType.winHandle
  52.             Range("a1").Offset(x, intOffset) = hwnd
  53.         Case winOutputType.winTitle
  54.             strText = String$(100, Chr$(0))
  55.             lngRet = GetWindowText(hwnd, strText, 100)
  56.             If lngRet > 0 Then
  57.                 Range("a1").Offset(x, intOffset) = Left$(strText, lngRet)
  58.             Else
  59.                 Range("a1").Offset(x, intOffset) = "N/A"
  60.             End If
  61.         Case winOutputType.winHandleClass
  62.             Range("a1").Offset(x, intOffset) = hwnd
  63.             strText = String$(100, Chr$(0))
  64.             lngRet = GetClassName(hwnd, strText, 100)
  65.             Range("a1").Offset(x, intOffset + 1) = Left$(strText, lngRet)
  66.         Case winOutputType.winHandleTitle
  67.             Range("a1").Offset(x, intOffset) = hwnd
  68.             strText = String$(100, Chr$(0))
  69.             lngRet = GetWindowText(hwnd, strText, 100)
  70.             If lngRet > 0 Then
  71.                 Range("a1").Offset(x, intOffset + 1) = Left$(strText, lngRet)
  72.             Else
  73.                 Range("a1").Offset(x, intOffset + 1) = "N/A"
  74.             End If
  75.         Case winOutputType.winHandleClassTitle
  76.             Range("a1").Offset(x, intOffset) = hwnd
  77.             strText = String$(100, Chr$(0))
  78.             lngRet = GetClassName(hwnd, strText, 100)
  79.             Range("a1").Offset(x, intOffset + 1) = Left$(strText, lngRet)
  80.             strText = String$(100, Chr$(0))
  81.             lngRet = GetWindowText(hwnd, strText, 100)
  82.             If lngRet > 0 Then
  83.                 Range("a1").Offset(x, intOffset + 2) = Left$(strText, lngRet)
  84.             Else
  85.                 Range("a1").Offset(x, intOffset + 2) = "N/A"
  86.             End If
  87.         End Select
  88.          'check for children
  89.        y = x
  90.         Select Case OutputType
  91.         Case Is > 4
  92.             GetWinInfo hwnd, intOffset + 3, OutputType
  93.         Case Is > 2
  94.             GetWinInfo hwnd, intOffset + 2, OutputType
  95.         Case Else
  96.             GetWinInfo hwnd, intOffset + 1, OutputType
  97.         End Select
  98.          'increment by 1 row if no children found
  99.        If y = x Then
  100.             x = x + 1
  101.         End If
  102.          'now get next window
  103.        hwnd = FindWindowEx(hParent, hwnd, vbNullString, vbNullString)
  104.     Wend
  105.      
  106. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement