Find Window Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Newbie Coder

    #1

    Find Window Question

    Hello Group

    I am trying to find a window using the known classname, which I got through
    Spy++

    The classname is'AfxWnd80s' & when I use FindWindow API to return the INT32
    with the window handle it returns 0 (zero)

    Example:

    Private Declare Function FindWindow Lib "user32" Alias "FindWindow A"
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32

    Dim i As Int32 = FindWindow("Afx Wnd80s", VbNullString)

    MessageBox.Show (i.ToString)

    This window displays maybe once a week so, I will have a long wait in which
    to test it

    Any Ideas?

    TIA

    Newbie Coder


  • Tom Shelton

    #2
    Re: Find Window Question

    On 2006-11-30, Newbie Coder <newbie_coder@p leasespamme.com wrote:
    Hello Group
    >
    I am trying to find a window using the known classname, which I got through
    Spy++
    >
    The classname is'AfxWnd80s' & when I use FindWindow API to return the INT32
    with the window handle it returns 0 (zero)
    >
    Example:
    >
    Private Declare Function FindWindow Lib "user32" Alias "FindWindow A"
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
    >
    Private Declare Auto Function FindWindow Lib "user32" _
    (Byval lpClassName As String, ByVal lpWindowName As String) As IntPtr

    Don't alias API calls anymore. That is a relic of the bad old days of VB6
    when VB could not call unicode api calls directly. Just declare the function
    Auto. The .NET marshaller is smart enough to figure out the proper function
    to call on the current platform. If you alias to the "A" function on a NT
    based platform (NT, 2000, XP, etc) - you are causing the runtime way more work
    then is necessary, because it will first convert your string from it's unicode
    format to ansi, and then pass that buffer to the api call. The api call will
    promptly convert the ansi string it was passed to unicode and delegate the
    call to the "W" function. The situation is even worse if the api call
    actually modifies the string (which this one doesn't)... Because then that whole process has to be
    reversed on top of the fact that because you used a system.string to recieve
    the buffer rather then a system.text.str ingbuilder, the vb.net marshaler get's
    involved to cover your butt from the fact that strings are immutable. In
    fact, using system.string as a api buffer will not work correctly in C#
    because it doesn't cover for you :)
    Dim i As Int32 = FindWindow("Afx Wnd80s", VbNullString)
    >
    Dim i As IntPtr = FindWindow ("AfxWnd80s" , Nothing)


    --
    Tom Shelton

    Comment

    • Newbie Coder

      #3
      Re: Find Window Question

      Thanks for your reply, but your code returns zero because I tried it before
      I posted the question

      All you have done is changed my Int32 to IntPtr

      However, I know the Marshall... but I was knocking togething a 2 min program
      I thought, but was wrong. Plus I have a VB background stending from VB 3

      It seems that the class I have to get is nested within another class

      So, I used FindWindow to get parent & then FindWindowEx passing the handle
      from the FindWindow as the first parameter.

      Example:

      Dim i as Int32 = FindWindow("[class name here]", VbNullString)

      Dim ii as Int32 = FindWindowEx(i, 0, "AfxWnd80s" , VbNullString)

      MessageBox.Show (ii.ToString) returns the wrong handle because there are more
      than one control with the 'AfxWnd80s' class

      Any ideas?


      Comment

      Working...