VB and VBA Users Source Code: Returning the parent window handle of a process
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Returning the parent window handle of a process
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, August 01, 2002
Hits:
1158
Category:
Windows API
Article:
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. ' [lStartSearchFromHwnd] If called recursively, use the parameter to specify the previously returned window handle. 'Outputs : Returns the handle of parent window owned by the specified process 'Author : Andrew Baker 'Date : 01/11/2000 13:17 'Notes : 'Revisions : 'Assumptions : Function FindProcessParentHwnd(ByVal lProcID As Long, Optional bReturnVisible As Boolean = True, Optional lStartSearchFromHwnd = -1) As Long Const GW_HWNDNEXT = 2, WS_VISIBLE = &H10000000, GWL_STYLE = (-16) Dim lTestHwnd As Long, lTestPID As Long, lStyle As Long Dim bFoundLastHwnd As Boolean '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 'Found a window with our Process id as it's process If lStartSearchFromHwnd > -1 Then 'Called recursively, check the window if past the last window If bFoundLastHwnd Then 'Found next window FindProcessParentHwnd = lTestHwnd Exit Do End If If lTestHwnd = lStartSearchFromHwnd Then 'Found previous window handle bFoundLastHwnd = True End If Else 'Found first matching window for the specified PID FindProcessParentHwnd = lTestHwnd 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 = FindProcessParentHwnd(GetCurrentProcessId) Debug.Print "Parent window handles owned by this process" Do While lParent Debug.Print lParent lParent = FindProcessParentHwnd(GetCurrentProcessId, , lParent) Loop End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder