Results 1 to 8 of 8

Thread: [RESOLVED] FindWindowEx trouble

  1. #1
    Lively Member
    Join Date
    Jul 10
    Posts
    93

    Resolved [RESOLVED] FindWindowEx trouble

    I've read a lot about this but can't seem to get the code to work.

    I don't have SPy++ but here the window I am trying to access:

    Name:  Capture.PNG
Views: 647
Size:  32.0 KB

    I have attempted multiple examples of this:

    Code:
    Option Explicit On
    'test module for accessing third party windows
    Module Module2
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
        Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
        Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
        Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    
        Public Sub Form_Load()
            Dim lRet As Long
    
            lRet = FindWindow(vbNullString, "Home (Search)")
            If lRet = 0 Then Exit Sub
            lRet = FindWindowEx(me.hwand, lRet, vbNullString, vbNullString)
            Do While lRet <> 0
                ShowWindowInfo(lRet)
                lRet = FindWindowEx(me.hwand, lRet, vbNullString, vbNullString)
            Loop
        End Sub
    
        Private Sub ShowWindowInfo(ByVal hWnd As Long)
            Dim lRet As Long
            Dim buffer As String
            Dim message As String
    
            message = "Window handle: " & hWnd & vbCrLf
    
            buffer = Space(256)
            lRet = GetWindowText(hWnd, buffer, 256&)
            message = message & "Window text: " & Left$(buffer, lRet) & vbCrLf
    
            buffer = Space(256)
            lRet = GetClassName(hWnd, buffer, 256&)
            message = message & "Class name: " & Left$(buffer, lRet) & vbCrLf
    
            Debug.Print(message)
        End Sub
    With this example I was expecting it to loop through all of the child windows of "Home (Search)". I'm getting an error on Me.hwnd since it is in a module. What do I need to replace Me with for this to execute properly?

  2. #2
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,251

    Re: FindWindowEx trouble

    There is no need for Me.hwnd, because the first FindWindowEx's parameter is refer to the parent window handle which you already get by FindWindow, read more details about FindWindowEx

    Code:
    Option Explicit On
    'test module for accessing third party windows
    Module Module2
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
        Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
        Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
        Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    
        Public Sub Form_Load()
            Dim lngParent As Long
            Dim lngChild As Long
    
            lngParent = FindWindow(vbNullString, "Home (Search)")
            If lngParent <> 0 Then
                lngChild = FindWindowEx(lngParent, 0, vbNullString, vbNullString)
                Do While lngChild <> 0
                    ShowWindowInfo(lngChild)
                    lngChild = FindWindowEx(lngParent, lngChild, vbNullString, vbNullString)
                Loop
            End If
        End Sub
    
        Private Sub ShowWindowInfo(ByVal hWnd As Long)
            Dim lRet As Long
            Dim buffer As String
            Dim message As String
    
            message = "Window handle: " & hWnd & vbCrLf
    
            buffer = Space(256)
            lRet = GetWindowText(hWnd, buffer, 256&)
            message = message & "Window text: " & Left$(buffer, lRet) & vbCrLf
    
            buffer = Space(256)
            lRet = GetClassName(hWnd, buffer, 256&)
            message = message & "Class name: " & Left$(buffer, lRet) & vbCrLf
    
            Debug.Print(message)
        End Sub
    
    End Module

  3. #3
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,136

    Re: FindWindowEx trouble

    'me.hwand' is spelt incorrectly. To access the Form's hwnd within a Module, prefix it with the Form's name (eg Form1.hwnd)

  4. #4
    Lively Member
    Join Date
    Jul 10
    Posts
    93

    Re: FindWindowEx trouble

    I tried your code 4x2y and I'm getting the following error:

    pinvoke stack imbalance detected

    lngChild = FindWindowEx(lngParent, 0, vbNullString, vbNullString)
    A call to PInvoke function 'Metrics!Metrics.Module2::FindWindowEx' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

    The spelling error was a mistake becasue I was in a hurry earlier, I didn't have the same spelling in my code.

  5. #5
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,136

    Re: FindWindowEx trouble

    Not sure about .Net but in VB6 all numeric literals are defaulted to Integer Types. I suspect this
    Code:
    lngChild = FindWindowEx(lngParent, 0, vbNullString, vbNullString)
    needs to be changed to something like this
    Code:
    lngChild = FindWindowEx(lngParent, CLng(0), vbNullString, vbNullString)

  6. #6
    Lively Member
    Join Date
    Jul 10
    Posts
    93

    Re: FindWindowEx trouble

    I'm getting the same error. I can f5 past it and the code is actually executing afterwards, but always breaks on that line.

  7. #7
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,251

    Re: FindWindowEx trouble

    Long in VB6 is Integer/Int32 in VB.NET, so replace Long in all API declaration to Integer/Int32
    Code:
    Option Explicit On
    
    Module Module2
        Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
        Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWndParent As Int32, ByVal hWndChildAfter As Int32, ByVal lpszClass As String, ByVal lpszWindow As String) As Int32
        Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Int32, ByVal lpString As String, ByVal cch As Int32) As Int32
        Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As Int32, ByVal lpClassName As String, ByVal nMaxCount As Int32) As Int32
    
        Public Sub Form_Load()
            Dim lngParent As Int32
            Dim lngChild As Int32
    
            lngParent = FindWindow(vbNullString, "Home (Search)")
            If lngParent <> 0 Then
                lngChild = FindWindowEx(lngParent, 0, vbNullString, vbNullString)
                Do While lngChild <> 0
                    ShowWindowInfo(lngChild)
                    lngChild = FindWindowEx(lngParent, lngChild, vbNullString, vbNullString)
                Loop
            End If
        End Sub
    
        Private Sub ShowWindowInfo(ByVal hWnd As Long)
            Dim lRet As Int32
            Dim buffer As String
            Dim message As String
    
            message = "Window handle: " & hWnd & vbCrLf
    
            buffer = Space(256)
            lRet = GetWindowText(hWnd, buffer, 256&)
            message = message & "Window text: " & Left$(buffer, lRet) & vbCrLf
    
            buffer = Space(256)
            lRet = GetClassName(hWnd, buffer, 256&)
            message = message & "Class name: " & Left$(buffer, lRet) & vbCrLf
    
            Debug.Print(message)
        End Sub
    
    End Module
    Last edited by 4x2y; Aug 23rd, 2012 at 07:09 AM.

  8. #8
    Lively Member
    Join Date
    Jul 10
    Posts
    93

    Re: FindWindowEx trouble

    I did not realize this, thanks works great!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •