Visual Basic Programming Code Examples Visual Basic > Windows and Controls Code Examples Returning the parent windows owned by a process Returning the parent windows owned by a process The following code demonstrates how to return all the top level (parent) window handles owned by a specific process ID. Option Explicit Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long 'Purpose : Returns a top level (parent) window handle for a specified process ID 'Inputs : lProcID The process ID to return a parent window handle of. ' Note, can be called recursively to retrieve all the ' parent windows owned by a process. ' [bReturnVisible] If True only returns top level windows which have the WS_VISIBLE style bit set. 'Outputs : Returns the handle of parent window owned by the specified process Function ProcessToParentWnd(ByVal lProcID As Long, Optional bReturnVisible As Boolean = True) As Long Const GW_HWNDNEXT = 2, WS_VISIBLE = &H10000000, GWL_STYLE = (-16) Static ssPreviousParents As String, slLastProcID As Long Dim lTestHwnd As Long, lTestPID As Long, lStyle As Long If lProcID = 0 Then 'Reset statics ssPreviousParents = "" slLastProcID = 0 Exit Function End If 'Find the first window lTestHwnd = FindWindow(ByVal 0&, ByVal 0&) Do While lTestHwnd <> 0 'Check if the window isn't a child If GetParent(lTestHwnd) = 0 Then 'Get the window's ProcessID Call GetWindowThreadProcessId(lTestHwnd, lTestPID) If lTestPID = lProcID Then If bReturnVisible Then 'Do not return hidden windows lStyle = GetWindowLong(lTestHwnd, GWL_STYLE) If (lStyle And WS_VISIBLE) = False Then 'Not a visible window lTestPID = -1 End If End If End If If lTestPID = lProcID Then If slLastProcID = lProcID Then 'Called with the same proc ID twice If InStr(1, ssPreviousParents, CStr(lTestHwnd) & "|") = 0 Then 'Return the next parent handle for this process ProcessToParentWnd = lTestHwnd ssPreviousParents = ssPreviousParents & ProcessToParentWnd & "|" slLastProcID = lProcID Exit Do End If Else 'New Proc ID ProcessToParentWnd = lTestHwnd ssPreviousParents = ProcessToParentWnd & "|" slLastProcID = lProcID Exit Do End If End If End If 'retrieve the next window lTestHwnd = GetWindow(lTestHwnd, GW_HWNDNEXT) Loop End Function 'Demonstration Sub Test() Dim lParent As Long 'Retrieve all the parent (top level) window handles owned by this process lParent = ProcessToParentWnd(GetCurrentProcessId) Debug.Print "Parent window handles owned by this process" Do While lParent Debug.Print lParent lParent = ProcessToParentWnd(GetCurrentProcessId) Loop 'Reset internal statics in function ProcessToParentWnd 0 End Sub