• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

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
0
jstrom122099
Asked:
jstrom122099
1 Solution
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now