Avatar of peispud
peispudFlag for Canada

asked on 

Function to return a type array containing al visible top level window captions and handles

Hi

I have a function that will populate a listbox  with the handles and window captions of all list of top level visible windows. The code works perfectly.

I would prefer that the results be returned in a type array (any type of array or structure)  so that the function can be called without the need of a listbox.  
I am hoping that someone can help me with the modifications needed on the two last functions in the module.  Just look for
'------------  Modifications needed below here please ----------------

Open in new window

Thank you for you r help.


'Form 1
Private Sub btnGetAllWindows_Click()
    Dim type_Info() As WindowInfo
    Typeinfo = GetAllTopWindows()        '         I would prefer this to work
End Sub

Open in new window



The code below here works perfectly so long as you have "MyListbox" in the form.

'Form 1             
Private Sub btnGetAllWindows_Click()
    MyListBox.RowSourceType = "Value List"
    Call GetAllTopWindows(Me.MyListBox)  '         This works
End Sub


Open in new window

'Module

Option Compare Database
Option Explicit

Public Const GWL_STYLE  As Long = -16&
Public Const WS_CAPTION As Long = &HC00000

Public Type WindowInfo
    hWnd As Long
    Name As String
End Type

Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowLongW Lib "user32.dll" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function IsWindowVisible Lib "user32.dll" (ByVal hWnd As Long) As Long
Public Declare Function GetWindowTextLengthW Lib "user32.dll" (ByVal hWnd As Long) As Long
Public Declare Function SysReAllocStringLen Lib "oleaut32.dll" (ByVal pBSTR As Long, Optional ByVal pszStrPtr As Long, Optional ByVal Length As Long) As Long
Public Declare Function GetWindowTextW Lib "user32.dll" (ByVal hWnd As Long, ByVal lpString As Long, ByVal nMaxCount As Long) As Long






Public Function FormatHex(ByVal Value As Long) As String
    FormatHex = "&H" & Right$("0000000" & Hex$(Value), 8&)
End Function

Public Function GetWindowCaption(ByVal hWnd As Long) As String
    Dim Lenght As Long
    Lenght = GetWindowTextLengthW(hWnd)
    If Lenght Then
        SysReAllocStringLen VarPtr(GetWindowCaption), , Lenght
        GetWindowTextW hWnd, StrPtr(GetWindowCaption), Lenght + 1&
    End If
End Function



'--------------  Modifications needed below here please -------------------

Public Function GetAllTopWindows(ByRef thelistbox As ListBox) As WindowInfo()    
    EnumWindows AddressOf EnumWindowsProc, VarPtr(thelistbox)
End Function



' ---------------Modification needed below here please ------------------------
Private Function EnumWindowsProc(ByVal hWnd As Long, ByRef lParam As ListBox) As Long   
    If (GetWindowLongW(hWnd, GWL_STYLE) And WS_CAPTION) = WS_CAPTION Then
        If IsWindowVisible(hWnd) Then
            lParam.AddItem FormatHex(hWnd) & " """ & GetWindowCaption(hWnd) & """"
        End If
    End If
    EnumWindowsProc = -True
End Function 

Open in new window



Microsoft AccessVBA

Avatar of undefined
Last Comment
peispud
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Just Substitute the listbox with a structure like Collection or Dictionary/
Avatar of peispud
peispud
Flag of Canada image

ASKER

I am sure that you are correct in you answer.  Thank you.

I've been trying and have not yet succeeded.
I've been trying to put the idea into a class module.

I'm sure that it's easy.  But not right away.  Some more guidance would be appreciated.
I'll keep working this though.
ASKER CERTIFIED SOLUTION
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of peispud
peispud
Flag of Canada image

ASKER

Ok...  I can do that.

Avatar of peispud
peispud
Flag of Canada image

ASKER

I have made progress.  I am almost there.
But I have hit a wall.

Everything works great when I call a function in a regular module.
But the same code fails in a class module.  
See 2nd code snippet,  *** Error here *****.    The error is "Invalid use of AddressOf operator"


' Form 1
Private Sub Command0_Click()

     '  --------------------- This call to a module works fine
     Dim MyWindowInformation As New Collection
     Set MyWindowInformation = GetAllTopWindows    '         This works
     
        
     '  ---------------------   This call to the class fails
     Dim www As Class_EnumerateWindows
     Set www = New Class_EnumerateWindows
     www.GetAllTopWindows
End Sub

Open in new window




' Regular Module ---- it works
' Class Module   ----  Error is generated 

Public Function GetAllTopWindows() As Collection
Set coll = New Collection
    EnumWindows AddressOf EnumWindowsProc, VarPtr(coll)   ' *** Error here *****
    Set GetAllTopWindows = coll
End Function



Private Function EnumWindowsProc(ByVal hWnd As Long, ByRef lParam As Collection) As Long
    If (GetWindowLongW(hWnd, GWL_STYLE) And WS_CAPTION) = WS_CAPTION Then
        If IsWindowVisible(hWnd) Then
               Dim MyData As New MyWinInfo
               MyData.Name = GetWindowCaption(hWnd)
               MyData.Handle = FormatHex(hWnd)
               coll.Add MyData
        End If
    End If
    EnumWindowsProc = -True
End Function

Open in new window

Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo