locked
Trying to understand GetNextWindow( ) RRS feed

  • Question

  • So I'll preface with the fact that I'm a super-n00b. I've taken a class in VB.Net and whereas I did really well in it, I don't think it gave me the kind of knowledge I was wanting to get from it. I'm trying to write a program to sling macros into a telnet session and have been trying to mess with some of the functions in user32.dll, in this case, GetNextWindow.

    A portion of my play-code:
    =====================================
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function GetNextWindow(ByVal hWnd As IntPtr, ByVal wCmd As String) As IntPtr

        End Function

        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function GetForegroundWindow() As IntPtr

        End Function



        Private Sub Button_GetNextWindow(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetNextWindow.Click

            Dim NextWindowHandle As IntPtr
            Dim CurrentWindowHandle As IntPtr

            ' Get handle for current window.
            CurrentWindowHandle = GetForegroundWindow()

            NextWindowHandle = GetNextWindow(CurrentWindowHandle, "GW_HWNDNEXT")

        End Sub
    ====================================

    I'm assuming it's choking on the second parameter, but I'm not sure. In looking at the parameters for this function, I was assuming that I pass in the second one as a string. My thought is I have to supply the handle for the current window, and then one of two possible strings indicating Next or Previous" for the window handle I want.

    Can somebody help educate me on this? Also, if somebody wouldn't mind pointing me to a good article on DllImport that is specfic to VB.Net, that would be great. All my searching keeps coming up with C# or C++ articles.

    Thanks for your time!
    Tuesday, February 10, 2009 5:17 AM

Answers

  • Well, the second parameter should not be a string. The GW_HWNDNEXT & GW_HWNDPREV are constant values defined in winuser.h available in Windows SDK. They're in C/C++ format for C/C++ developers.

    The good news is, there are always ways to get enough information on what you want:

    GetNextWindow function document: http://msdn.microsoft.com/en-us/library/ms633509(VS.85).aspx; this explains what the function does.

    For the real value of GW_* constants, there is a handy third party website to go: http://www.pinvoke.net/default.aspx/Constants/GW%20-%20GetWindow()%20Constants.html

    Note: This is a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    From there, we can find:

    1 Public Enum GetWndConsts  
    2     GW_HWNDFIRST = 0   
    3     GW_HWNDLAST = 1   
    4     GW_HWNDNEXT = 2   
    5     GW_HWNDPREV = 3   
    6     GW_OWNER = 4   
    7     GW_CHILD = 5   
    8     GW_ENABLEDPOPUP = 6  
    9     GW_MAX = 6  
    10 End Enum 

    So you do it like this:

    1 NextWindowHandle = GetNextWindow(CurrentWindowHandle, GetWndConsts.GW_HWNDNEXT) 

    And the article you asked for:
    Walkthrough: Calling Windows APIs

    Regards,

    Jie
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, February 10, 2009 9:09 AM
  • Hi

    I think it is the second parameter that is incorrect. Rather than a string, it should be declared as an Uint32 and the GW_HWNDNEXT is actually a constant value of 2.

    Hope this helps

    Kev
    Tuesday, February 10, 2009 9:09 AM

All replies

  • Well, the second parameter should not be a string. The GW_HWNDNEXT & GW_HWNDPREV are constant values defined in winuser.h available in Windows SDK. They're in C/C++ format for C/C++ developers.

    The good news is, there are always ways to get enough information on what you want:

    GetNextWindow function document: http://msdn.microsoft.com/en-us/library/ms633509(VS.85).aspx; this explains what the function does.

    For the real value of GW_* constants, there is a handy third party website to go: http://www.pinvoke.net/default.aspx/Constants/GW%20-%20GetWindow()%20Constants.html

    Note: This is a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    From there, we can find:

    1 Public Enum GetWndConsts  
    2     GW_HWNDFIRST = 0   
    3     GW_HWNDLAST = 1   
    4     GW_HWNDNEXT = 2   
    5     GW_HWNDPREV = 3   
    6     GW_OWNER = 4   
    7     GW_CHILD = 5   
    8     GW_ENABLEDPOPUP = 6  
    9     GW_MAX = 6  
    10 End Enum 

    So you do it like this:

    1 NextWindowHandle = GetNextWindow(CurrentWindowHandle, GetWndConsts.GW_HWNDNEXT) 

    And the article you asked for:
    Walkthrough: Calling Windows APIs

    Regards,

    Jie
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, February 10, 2009 9:09 AM
  • Hi

    I think it is the second parameter that is incorrect. Rather than a string, it should be declared as an Uint32 and the GW_HWNDNEXT is actually a constant value of 2.

    Hope this helps

    Kev
    Tuesday, February 10, 2009 9:09 AM
  • I just wanted to say thank you. This was immensely helpful.
    Thursday, February 12, 2009 4:52 AM