Code: Select all
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
Private
Code: Select all
Global test_hwnd.l, test_pid.l, test_thread_id.l
Procedure InstanceToWnd(target_pid.l)
; Dim test_hwnd.l
; Dim test_pid.l
; Dim test_thread_id.l
;Find the first window
test_hwnd = FindWindow_(0,0)
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
                Break ;Exit Do
              EndIf
        EndIf
        ;retrieve the Next window
        test_hwnd = GetWindow_(test_hwnd, GW_HWNDNEXT)
Wend
ProcedureReturn test_hwnd
EndProcedure
Code: Select all
Debug InstanceToWnd(GetPidProcess("notepad.exe"))
This has got to be one of the slowest things in the world, I had time to make a picture of iced tea and a sandwich before it finished. Anyway, it begs the question as to how to retrieve a window handle from a processID when all you know is the processID. So if you have the processID and nothing else (no window titles or anything and its not a window you made), how can you get the hWnd in a painless way from the processID (I know there can be many processes associated with an executable - maybe this is why it would be difficult?). Seems difficult if not impossible to do, I guess you could enum everything but this takes time too. Any one have any ideas?


