How to get a PID from a hProcess handle

Posted on 2002-03-05
Last Modified: 2008-03-04
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
Question by:JEBO
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 52

Expert Comment

by:Ryan Chong
ID: 6841129
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
    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)
End Sub

Accepted Solution

Bahnass earned 50 total points
ID: 6841373
I think U mean sei.hProcess in next example

its here in my paq
I was asking about it to wait for the prog

   cbSize As Long
   fMask As Long
   hwnd As Long
   lpVerb As String
   lpFile As String
   lpParameters As String
   lpDirectory As String
   nShow As Long
   hInstApp As Long
   lpIDList As Long
   lpClass As String
   hkeyClass As Long
   dwHotKey As Long
   hIcon As Long
   hProcess As Long
End Type
Private Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpExecInfo As _
Const SE_ERR_FNF = 2
Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal _
   dwMilliseconds As Long) As Long
Const WAIT_TIMEOUT = &H102

' *** Place the following code inside window Form1. ***
Private Sub Command1_Click()
   Dim sei As SHELLEXECUTEINFO  ' structure used by the function
   Dim retval As Long  ' return value
   ' Load the information needed to open C:\Docs\readme.txt
   ' into the structure.
   With sei
       ' Size of the structure
       .cbSize = Len(sei)
       ' Use the optional hProcess element of the structure.
       ' Handle to the window calling this function.
       .hwnd = Form1.hwnd
       ' The action to perform: open the file.
       .lpVerb = "open"
       ' The file to open.
       .lpFile = "C:\a.txt"
       ' No additional parameters are needed here.
       .lpParameters = ""
       ' The default directory -- not really necessary in this case.
       .lpDirectory = "C:\"
       ' Simply display the window.
       .nShow = SW_SHOWNORMAL
       ' The other elements of the structure are either not used
       ' or will be set when the function returns.
   End With
   ' Open the file using its associated program.
   retval = ShellExecuteEx(sei)
   If retval = 0 Then
       ' The function failed, so report the error.  Err.LastDllError
       ' could also be used instead, if you wish.
       Select Case sei.hInstApp
       Case SE_ERR_FNF
           MsgBox "File not found"
       Case SE_NOASSOC
            MsgBox "no program is associated to this file"
       Case Else
           MsgBox "An unexpected error occured."
       End Select
       ' Wait for the opened process to close before continuing.  Instead
       ' of waiting once for a time of INFINITE, this example repeatedly checks to see if the
       ' is still open.  This allows the DoEvents VB function to be called, preventing
       ' our program from appearing to lock up while it waits.
           retval = WaitForSingleObject(sei.hProcess, 0)
       Loop While retval = WAIT_TIMEOUT
       MsgBox "the program is ended"
   End If
End Sub
LVL 49

Expert Comment

ID: 7591753
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

Expert Comment

ID: 7624076
Finalized as proposed


Community Support Moderator
Experts Exchange

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2005 text editor 10 57
how to Classify of email (pdf attachments) 7 57
Word formfield existence 3 16
2 Global Vars, 1 List Box 4 34
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question