Use the EnumWindows function. Next example will retrieve the Name, Class and hWnd of all the running windows.
Code for a Module
Code for a Form with a CommandButtonCode:Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim Length As Long Dim Buffer As String Dim Temp As String Dim sClass As String Dim icLength As Integer Static iCount As Integer iCount = iCount + 1 Length = GetWindowTextLength(hwnd) + 1 sClass = Space(255) If Length > 1 Then Buffer = Space(Length) 'Get the name GetWindowText hwnd, Buffer, Length 'Get the Class icLength = GetClassName(hwnd, sClass, 255) 'Display the Name, Class and hWnd sClass = Left(sClass, icLength) Debug.Print Left(Buffer, Length - 1) & " - " & sClass & " - " & hwnd End If EnumWindowsProc = 1 End Function
Code:Private Sub Command1_Click() EnumWindows AddressOf EnumWindowsProc, 0 End Sub




Reply With Quote