troubleshooting Question

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

Avatar of peispud
peispudFlag for Canada asked on
Microsoft AccessVBA
5 Comments1 Solution28 ViewsLast Modified:

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


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

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 5 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 5 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004