Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to get a PID from a hProcess handle

Posted on 2002-03-05
Medium Priority
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 53

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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

719 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