SendMessage problem!

Hi!

Why isn't this working:

In a module:

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_GETTEXT = &HD

Public Function EnumWindowProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Form1.List1.AddItem hwnd
Call EnumChildWindows(hwnd, AddressOf EnumChildProc, &H0)
EnumWindowProc = 1
End Function

Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Form1.List2.AddItem hwnd

Dim Buffer As String * 255
If GetWindowLong(hwnd, -16) And &H20 <> 0 Then ' If a textbox
        SendMessage hwnd, WM_GETTEXT, 255, Buffer
        Debug.Print Buffer
End If

EnumChildProc = 1
End Function

In a form (there should be 2 listboxes there):

In a commandbutton:

Call EnumWindows(AddressOf EnumWindowProc, &H0)

This just give me an illegal operation...


/Johan
jstrom122099Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Erick37Commented:
Change the SendMessage to

Public Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, ByVal lParam As String) _
    As Long

'Note the last parameter is declared ByVal As String.

And a little modification to your function:

Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Form1.List2.AddItem hwnd
    Dim Buffer As String
    If GetWindowLong(hwnd, -16) And &H20 <> 0 Then ' If a textbox
        Buffer = String(255, Chr(0))
        SendMessageStr hwnd, WM_GETTEXT, 255, Buffer
        Buffer = Left(Buffer, InStr(1, Buffer, Chr(0)) - 1)
        If Len(Buffer) <> 0 Then
            Debug.Print Buffer
        End If
    End If
    EnumChildProc = 1
End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.