Solved

How to get a PID from a hProcess handle

Posted on 2002-03-05
4
766 Views
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
Jesper
0
Comment
Question by:JEBO
[X]
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
4 Comments
 
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
    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
0
 
LVL 3

Accepted Solution

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

its here in my paq
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=visualbasic&qid=20272579
I was asking about it to wait for the prog






Private Type SHELLEXECUTEINFO
   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
Const SEE_MASK_NOCLOSEPROCESS = &H40
Const SW_SHOWNORMAL = 1
Private Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpExecInfo As _
   SHELLEXECUTEINFO) As Long
Const SE_ERR_FNF = 2
Const SE_ERR_NOASSOC = 31
Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal _
   dwMilliseconds As Long) As Long
Const INFINITE = &HFFFF
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.
       .fMask = SEE_MASK_NOCLOSEPROCESS
       ' 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
   Else
       ' 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.
       Do
           DoEvents
           retval = WaitForSingleObject(sei.hProcess, 0)
       Loop While retval = WAIT_TIMEOUT
       MsgBox "the program is ended"
   End If
End Sub
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7591753
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
0
 

Expert Comment

by:modulo
ID: 7624076
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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
Course of the Month9 days, 13 hours left to enroll

623 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