I'm starting an application using CreateProcess which gives me
hProcess, hThread, dwProcessID and dwThreadID. But how do I use these
to get the window handle so I can use other API calls such as
SetForegroundWindow which takes the window handle as an argument?
Thanks for any help,
Mong
Strangely there does not seem to be the opposite of
GetWindowThreadProcessId
Here is something that runs through topmost Windows and locates the
one with the same process ID
- not my code
- if you are using CreateProcess then you'll follow this
' --- This gives a hWnd from a Process ID
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd,
test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function