FindWindow API in VB2005 Express..??

Hi All,

This is my 1st post here. I have managed to figure out all my other problems so far, until now :) I am coming to VB2005Express from VB6...quite a jump, I know. I cannot figure out what I am doing wrong in this snippet. The return value in VB2005 Express is totally 100% wrong (I am using an API SPy I wrote several years ago to check my numbers. It has always been correct).

In my module, I have this:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

On My form I have a button & the click event is:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Win1 As Long

Win1& = 0

Win1& = FindWindow("Afx:400000:8:10011:0:c067f", Microsoft.VisualBasic.vbNullString)

MsgBox(Str$(Win1&))

End Sub

Each time I click the button, Win1& not only returns a different value, it is never the correct value. By the way, I am converting this routine from a VB4/VB5/VB6 routine that I have been using for years, so I know it's a working routine...at least in prior VB's.

Thanks in advance,

-Ron



Answer this question

FindWindow API in VB2005 Express..??

  • Ron Carr

    I appreciate both of your answers. I changed the declaration to IntPtr.

    However, I am still not getting the right results. Here is my call from the button click. I know this has to be where the problem is. I am getting a return value of 0 or the program crashes totally. The 1st snippet is what crashes my program out:

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim Win1

    Win1 = FindWindow("Afx:400000:8:10011:0:1a0aef", Microsoft.VisualBasic.vbNullString)

    MsgBox(Str$(Win1))

    End Sub

    This snippet returns "0":

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim Win1 As Integer

    Win1 = FindWindow("Afx:400000:8:10011:0:1a0aef", Microsoft.VisualBasic.vbNullString)

    MsgBox(Str$(Win1))

    End Sub

    Thanks in advance for any/all help :)

    -Ron


  • Dennis Gray

    I think Mattias is right - from VB 2002 onwards, an integer is now a 32 bit integer (it used to be a 16 bit integer in vb6) and a long is a 64 bit integer (used to be a 32 bit integer).

    HWND elements are typed in windows as pointer to void - this means their size on a 32 bit OS will be 32 bits - since you're using a long, which is 64 bits, the return value will be messed up. While you could use an integer, using an Intptr is more correct - if you ever need to port your code to a 64 bit OS, the intptr declaration will still work, a declaration using an integer will not. (Intptr size is OS dependent, and will be the size of a pointer in that OS)

    Side note - HWND value used to be the memory address to the window they represent - however, I think I've read somewhere that more recent versions of windows return a unique number, but not related to the memory address of the window), so if you're playing with C++, do not try to dereference them :)



  • Sohrab

    Str$ doesn't know how to convert an intptr to a string - use cstr instead, or intptr's toint32 method to print it as an integer

    It seems though that your code isn't finding the window you're looking for (returning 0 implies a failure to find the specified window) - does the legacy (vb6) code find it



  • Joris van de Klundert

    I figured it out! I don't know why I didn't think of this before, but I plugged 1 of my VB5 projects into VB2005 to see what the upgrade gave me. I copied & pasted those results into my program & voila! It works :) Just thought I'd share my results with everyone. This works just like my VB5 & VB6 code does. Thanks again for all the help.

    'In the module, I have this...

    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

    Public 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

    'On My Form, I have this...

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim Win1 As Integer

    Dim Win2 As Integer

    Win1 = FindWindow("AOL Frame25", vbNullString)

    Win2 = FindWindowEx(Win1, 0, "MDIClient", vbNullString)

    MsgBox(Str$(Win2))

    End Sub

    The return value for Win2 is exactly the same as what I got in my GUI spy (that I wrote in VB5) with no variance.

    Thanks again for the help,

    -Ron


  • jschroedl

    OK,

    Here is how I do it in VB6. I am going to use an AOL window just for reference:

    In My Module:

    Public 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

    In a Form, I fire this event with a command button:

    Win1& = FindWIndow("AOL Frame25", VbNullString)

    Win2& = FindWindowEx(Win1&, 0&, "AOL Child", VbNullString)

    This coding is flawless in VB4, VB5, and VB6. VB2005...laughs at me for even trying this :)

    Thanks in advance,

    -Ron


  • normschaef

    Yes, my VB code finds it with ease. I am sorry to be a pest...:) I want to learn VB2005. It looks like a great VB version :) I am sure to have many, many more questions as I go & I thank you for your kindness & help :)

    -Ron


  • Angz

    Well, I'm out of ideas - the call to the api seems correct - could you post the vb6 declaration and call you had maybe I can figure something out of it.

    Also, which window is this you're trying to get Is it from an application of yours, a standard windows program, a third party program



  • avenue

    The declaration should be

    Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr



  • FindWindow API in VB2005 Express..??