How to Find a Window Handle Based on an Instance Handle

ID: Q127030


The information in this article applies to:


SUMMARY

The Shell function in Microsoft Visual Basic is used to execute an application. Often, it is useful to get a Window handle (hWnd) to the application so you can manipulate it using the Windows APIs. Unfortunately, the Shell function returns an Instance handle (hInstance), which is different from a Window handle. This article shows by example how to use the GetWinHandle() function to return a Window handle based on an Instance handle.


MORE INFORMATION

By using the FindWindow() and GetWindow() APIs, you can loop through the Window handle list. For each window handle, you can check to see if it has a parent window with GetParent(). If the Window handle does not have a parent handle, you've reached the main window for an application. You can call GetWindowWord() to check the instance handle of the application versus the instance handle received from the Shell() function.

Step-by-Step Example

  1. Start a new project in Visual Basic (ALT, F, N). Form1 is created by default.


  2. Add a new module to the project (ALT, F, M), and enter the following declarations and function:
    
       ' Constants used
       Global Const GW_HWNDNEXT = 2
       Global Const GWW_HINSTANCE = (-6)
    
       ' Enter each of the following declarations as one, single line:
    
       Declare Function GetParent Lib "User" (ByVal hWnd As Integer) As Integer
       Declare Function GetWindow Lib "User" (ByVal hWnd As Integer,
          ByVal wCmd As Integer) As Integer
       Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer,
          ByVal nIndex As Integer) As Integer
       Declare Function FindWindow Lib "User" (ByVal lpClassName As Any,
          ByVal lpWindowName As Any) As Integer
       Declare Function GetWindowText Lib "User" (ByVal hWnd As Integer,
          ByVal lpString As String, ByVal aint As Integer) As Integer
    
       Function GetWinHandle (hInstance%) As Integer
          ' Function receives an instance handle as a parameter and returns
          ' the windows handle of the window with a matching instance handle.
          Dim tempHwnd%
    
          ' Grab the first window handle that Window's finds:
          tempHwnd% = FindWindow(0&, 0&)
    
          ' Loop until there are no more window handles:
          Do Until tempHwnd% = 0
            ' Check if you have the applications Parent window:
            If GetParent(tempHwnd%) = 0 Then
    
                 ' Check the instance handle for the app:
                 If hInstance% = GetWindowWord(tempHwnd%, GWW_HINSTANCE) Then
                   ' Found a match:
                   GetWinHandle = tempHwnd%
                   Exit Do
                 End If
             End If
             tempHwnd% = GetWindow(tempHwnd%, GW_HWNDNEXT)
          Loop
       End Function 


  3. Add a command button (Command1) to Form1, and place the following code in its Click event.
    
       Sub Command1_Click ()
          Dim hInst As Integer         ' Instance handle from Shell function.
          Dim hWndApp As Integer       ' Window handle from GetWinHandle.
          Dim buffer As String         ' Holds caption of Window.
          Dim numChars As Integer      ' Count of bytes returned.
    
          ' Shell to an application and get its window handle:
          hInst = Shell("calc.exe")
          hWndApp = GetWinHandle(hInst)
    
          ' Verify that you have the correct handle by displaying
          ' its window caption in a message box:
          buffer = Space$(128)
          numChars = GetWindowText(hWndApp, buffer, Len(buffer))
    
          MsgBox "You shelled to the Application: " & Left$(buffer, numChars)
       End Sub
     


  4. Press the F5 key to run the application. Click the command button to see that the Calculator is shelled to and to see a message box displaying "You shelled to the Application: Calculator."


Additional query words: 2.00 3.00


Keywords          : kbcode 
Version           : 2.00 3.00
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: May 26, 1999