Link to home
Start Free TrialLog in
Avatar of JEBO
JEBO

asked on

How to get a PID from a hProcess handle

Hi all

I am using ShellExecuteEx, to launch documents (and leave it to Windows to find the correct application).

From the ShellExecuteEx I get an hProcess handle.

How do I (if possible) "convert" the hProcess handle to the corresponding PID?

Best regards
Jesper
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Not very sure about this but you can try:

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long

Private Sub InstanceToWnd()
    Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
    'Find the first window
    List1.Clear
    test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
    Do While test_hwnd <> 0
        If IsWindowEnabled(test_hwnd) Then
            'If test_hwnd = Me.hwnd Then MsgBox test_thread_id & " : " & test_hwnd
            test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
   
        List1.AddItem test_thread_id & " : " & test_hwnd
        End If
       ' End If
        'retrieve the next window
        test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    Loop
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Bahnass
Bahnass
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi JEBO,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Bahnass's comment(s) as an answer.

JEBO, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
Avatar of modulo
modulo

Finalized as proposed

modulo

Community Support Moderator
Experts Exchange