Results 1 to 3 of 3

Thread: [RESOLVED] Problem with FindWindow API

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    17

    Resolved [RESOLVED] Problem with FindWindow API

    I use ShellExecute to launch an application from VB6. I then use FindWindow to get the application's window handle. Using Spy++ I know the class and the window caption, which I input into the FindWindow call.
    What happens is that FindWindow returns 0 and no window caption BUT, if I start a new instance of the application (by clicking on a launch button whose click procedure does the ShellExecute), then FindWindow returns a handle of the first instance. This is very reproducible but certainly not what I am after.
    Below is the code I have in my command click procedure:

    Private Sub Command1_Click()
    Dim r As Long 'Variable to store the response string
    Dim buffer As String
    Dim numChars As Integer
    Dim hwndPHD As Long

    r = ShellExecute(0, "Open", "C:\Program Files\PHDGuiding\PHD.exe", 0, 0, SW_SHOWNORM)
    lbltask_ID.Caption = Hex$(r)
    hwndPHD = FindWindow("wxWindowClassNR", "PHD Guiding 1.10.0 - www.stark-labs.com (Log active)")

    'Debug.Print Hex$(hwndPHD)
    If hwndPHD <> 0 Then
    ' Init buffer
    buffer = Space$(128)
    ' Get caption of window
    numChars = GetWindowText(hwndPHD, buffer, Len(buffer))
    lblhWnd.Caption = Hex$(hwndPHD)
    ' Display window's caption
    lblWinCaption.Caption = Left$(buffer, numChars)
    End If
    End Sub


    All the API declarations are done in a control module and I get no error when running the application in the IDE.
    I do not understand what is going on and will welcome any insight
    Thanks
    Olivier

  2. #2
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: Problem with FindWindow API

    well this probably isn't the problem, but i do see a programming situation to avoid.. GetWindowText API should return a LONG integer for any win32 OS, you dimmed numChars as integer.

    also you may want to put some do of loop to wait so many seconds for the Shell'd app to load the window with a doevents before you attempt to find it.
    simple example..
    Code:
    Dim st As Single
    Dim numChars As Long
    
    st = Timer
    Do
        Doevents  <--- EDIT: placed the 'obvious' i mentioned above into my code
        numChars = GetWindowText(hwndPHD, buffer, Len(buffer))
    Loop Until numChars Or (Timer - st >= 2)
    Last edited by Billy Conner; Jul 12th, 2009 at 10:30 PM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    17

    Re: Problem with FindWindow API

    Thanks.
    I found my problem. I thought it was timing related and added a delay but that did not do it. The solution was to place a Do Events and a loop polling FindWindow until it returned a non zero value. It now works fine.
    I also corrected the numChars formatting, thanks for the input
    Olivier

Posting Permissions

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



Click Here to Expand Forum to Full Width