Sign in to follow this  
ed209

[.net] enumerating child windows

Recommended Posts

ed209    122
I'm trying to enumerate all the child windows of a window but none the the child window's handles are being listed! here is my code
Public Class Form1

    Declare Function EnumChildWindows Lib "user32" Alias "EnumChildWindows" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long


    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
    ByVal lParam As String) As Integer

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, _
    ByVal lpsz2 As String) As Integer

    Const WM_GETTEXT As Long = &HD
    Const WM_GETTEXTLENGTH As Integer = &HE
    Const WM_SETTEXT = &HC

    Dim whwnd As Integer
    Dim hwnd_box As Integer
    Dim txt As String
    Dim j As Integer
 
    

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
 whwnd = FindWindow(vbNullString, "Form1")        

 EnumchildWindow(whwnd, 0)

EnumChildWindows(whwnd, AddressOf EnumchildWindow, 0)

                   
    End Sub




    Function EnumchildWindow(ByVal hChild As Long, ByVal lParam As Long) As Long

        txt = Space(256)
        j = SendMessage(hwnd_box, WM_GETTEXT, 255, txt)
        txt = Mid(txt, 1, j)

        TextBox1.Text += txt + vbNewLine

        EnumChildWindow = 1

    End Function

end class
[Edited by - ed209 on August 11, 2008 9:06:19 AM]

Share this post


Link to post
Share on other sites
ed209    122
Nah, cause I want to get the child windows from external programs too.

Can someone PLEASE tell me what i'm doing wrong or test out my code?

Share this post


Link to post
Share on other sites
mutex    1111
Your pinvoke definitions are wrong. Given the following definition for EnumChildWindows:

BOOL CALLBACK EnumChildProc(
HWND hwnd,
LPARAM lParam
);

BOOL EnumChildWindows(
HWND hWndParent,
WNDENUMPROC lpEnumFunc,
LPARAM lParam
);
Platform Invoke Data Types indicates that your pinvoke definition should be:

Delegate Function EnumWindowsProc(ByVal hwnd as IntPtr, ByVal lParam as IntPtr) as Integer

Declare Function EnumChildWindows Lib "user32" Alias "EnumChildWindows" (ByVal hWndParent As IntPtr, ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As IntPtr) As Integer
Remember that in VB (and .NET), Long is not the same as Integer. Also note that sizeof(LPARAM) is 4 on x86 but 8 on x64. You therefore have to use IntPtr for LPARAM.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this