[RESOLVED] FindWindow api problem-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] FindWindow api problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    217

    Resolved [RESOLVED] FindWindow api problem

    i have used

    FindWindow, GetWindow , GetClassName ,GetWindowTextLength , GetWindowText, PostMessage

    APIs to implement BM_CLICK (to click button of another exe and its working fine)


    But after closing vbp, im getting windows debug window !? (if exe used then it will be closed)


    Any idea ?

    thank you.

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: FindWindow api problem

    Can you give more details please? Such as the steps needed to replicate the issue, the OS used, etc.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: FindWindow api problem

    Do you mean that the VB6 IDE is aborting when you close the Program?
    Perhaps you could post your code. The most common reason when using APIs is not handling String Arguments properly.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    217

    Re: FindWindow api problem

    Code:
    'form
    
    Const GW_CHILD = 5
    Const GW_HWNDNEXT = 2
    
    Private Const BM_CLICK = &HF5
    
    Private Sub Command1_Click()
    On Error GoTo a1
        GetXLDESKChildWindows
    Exit Sub
    a1:
    MsgBox Err.Description
    End Sub
    
    
    Sub GetXLDESKChildWindows()
    On Error GoTo b1
    Dim hWndParent As Long, hWndDskTop As Long, hWndChild As Long, Ret As Long
    
    Dim strBuffer As String * 250
    Dim i As Long
        hWndDskTop = FindWindow(vbNullString, "F1")
        hWndChild = GetWindow(hWndDskTop, GW_CHILD)
        i = 1
        Do Until hWndChild = 0
            GetClassName hWndChild, strClass, 250
            Ret = GetWindowTextLength(hWndChild)
            
            strBuffer = Space(Ret)
            GetWindowText hWndChild, strBuffer, Ret + 1
    
            If hWndChild <> 0 Then
                Call PostMessage(hWndChild, BM_CLICK, 0&, ByVal 0&)
            End If
    
            hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
    
            i = i + 1
        Loop
    Exit Sub
    b1:
    MsgBox Err.Description
    
    End Sub
    
    
    
    
    'Module
    
    Public Declare Function FindWindow Lib "user32" _
                                Alias "FindWindowA" _
                                (ByVal lpClassName As String, _
                                ByVal lpWindowName As String) _
                                As Long
                                
    'Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds 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
    
    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 Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd 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 GetClassName _
        Lib "user32" _
            Alias "GetClassNameA" ( _
                ByVal hwnd As Long, _
                ByVal lpClassName As String, _
                ByVal nMaxCount As Long) _
    As Long
    
    Public Declare Function GetWindowText _
        Lib "user32" _
            Alias "GetWindowTextA" ( _
                ByVal hwnd As Long, _
                ByVal lpString As String, _
                ByVal cch As Long) _
    As Long
    
    Public Declare Function GetWindowTextLength _
        Lib "user32" _
            Alias "GetWindowTextLengthA" ( _
                ByVal hwnd As Long) _
    As Long
    
    Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
        Dim A$, G$, L&
        A = Space$(128)
        L = GetClassName(hwnd, A, 128)
        G = Left(A, L)
        
        If LCase$(G) <> "" Then
        fndTIT = Left(A, L): fndHWN = hwnd
        
        Else
        EnumChildProc = 1 'if not found...
        End If
    End Function

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    217

    Re: FindWindow api problem

    Name:  er.JPG
Views: 465
Size:  16.0 KB

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: FindWindow api problem

    Code:
    GetClassName hWndChild, strClass, 250
    will almost certainly give you a problem as strClass is not defined and not initialised. You should define it and initialise it in a similar fashion to strBuffer (e.g. set it to 250 spaces) before calling the API.

    I strongly recommend you put 'Option Explicit' as the first line of all your Forms and Modules; using that statement will cause the Compiler to warn you if any variables have not been defined and you could potentially avoid problems like this.
    Last edited by Doogle; Dec 4th, 2012 at 02:19 AM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    217

    Re: FindWindow api problem

    Thanks, problem solved after defining strClass

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.