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,
Printable View
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,
Absolutely:
Call this function like this: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
Code:Dim lngHwnd As Long
lngHwnd = GetHwndFromProcess(ValidProcessId)