Jump to content
  • Advertisement
Sign in to follow this  
ed209

[.net] enumerating child windows

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
If you find yourself working with such code a lot, make sure you memorize PInvoke.net. It's a wonderful resource for determining everything you need to know for interop.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!