Solved

How to get a PID from a hProcess handle

Posted on 2002-03-05
4
720 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
4 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Cannot enter table caption text in Word protected document 6 70
MsgBox 2 36
Run code from text file in vb 1 32
Copy a row 12 52
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now