Results 1 to 2 of 2

Thread: hWnd / Process ID

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 1999
    Location
    California
    Posts
    115

    Post

    Is there a way to get the Process ID from the window's hWnd or get the hWnd of a window with a process id?

    Thanks,

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Absolutely:
    Code:
    Private Const GW_HWNDFIRST = 0
    Private Const GW_HWNDNEXT = 2
    Private Const GW_CHILD = 5
    
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
    
    Public Function GetHwndFromProcess(p_lngProcessId As Long) As Long
        Dim lngDesktop As Long
        Dim lngChild As Long
        Dim lngChildProcessID As Long
        
        On Error Resume Next
        
        'get the hWnd of the desktop
        lngDesktop = GetDesktopWindow()
    
        lngChild = GetWindow(lngDesktop, GW_CHILD)
        
        Do While lngChild <> 0
        
            'get the ThreadProcessID of the window
            Call GetWindowThreadProcessId(lngChild, lngChildProcessID)
            
            'if ProcessId matches the parameter passed
            'then return that value
            If lngChildProcessID = p_lngProcessId Then
                GetHwndFromProcess = lngChild
                Exit Do
            End If
            
            'not found, continue enumeration
            lngChild = GetWindow(lngChild, GW_HWNDNEXT)
        Loop
    End Function
    Call this function like this:

    Code:
    Dim lngHwnd As Long
    
    lngHwnd = GetHwndFromProcess(ValidProcessId)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width