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
LVL 50

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 78
Excel - Save a copy of work book 13 88
maro to copy and paste from one worksheet to another based on a condition 11 54
vbModal 12 57
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…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

808 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