'------------ Modifications needed below here please ----------------
'Form 1
Private Sub btnGetAllWindows_Click()
Dim type_Info() As WindowInfo
Typeinfo = GetAllTopWindows() ' I would prefer this to work
End Sub
'Form 1
Private Sub btnGetAllWindows_Click()
MyListBox.RowSourceType = "Value List"
Call GetAllTopWindows(Me.MyListBox) ' This works
End Sub
'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
ASKER
ASKER
ASKER
' 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
' 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
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.
TRUSTED BY