VB and VBA Users Source Code: Returning the Process Id of an Exe
[
Home
|
Contents
|
Search
|
Reply
| Previous |
Next
]
VB/VBA Source Code
Returning the Process Id of an Exe
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, April 27, 2005
Hits:
3493
Category:
Windows API
Article:
The code below searches for a process and returns the process Id (or PID) given the process or EXE name to search for. Note, a demonstration routine can be found at the bottom of this post. Option Explicit Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long Private Declare Function EnumProcesses Lib "PSAPI.DLL" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long Private Declare Function GetModuleFileNameExA Lib "PSAPI.DLL" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long Private Declare Function EnumProcessModules Lib "PSAPI.DLL" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long 'Purpose : Returns the Id of the first matching process. 'Inputs : sExeName The name of the Exe (or image) to return the PID (process Id) of. ' [lStartFromPID] If called recursively, use this parameter to specify the ' PID returned from the previous call. 'Outputs : Returns the matching process id or -1 if a match was not found. 'Author : Andrew Baker (copyright www.vbusers.com) 'Date : 31/Jan/2002 'Notes : 'Revisions : Public Function FindProcessId(ByVal sExeName As String, Optional lStartFromPID = -1) As Long Const clInitNumProcesses As Long = 500 Const MAX_PATH = 260, PROCESS_QUERY_INFORMATION = 1024, PROCESS_VM_READ = 16 Dim sModuleName As String * MAX_PATH, sProcessNamePath As String, sProcessName As String Dim alMatchingProcessIDs() As Long Dim alModules(1 To 400) As Long Dim lBytesReturned As Long, lNumMatching As Long, lArraySize As Long Dim lNumProcesses As Long, lBytesNeeded As Long, alProcIDs() As Long Dim lHwndProcess As Long, lThisProcess As Long, lRet As Long Dim bPastLastMatch As Boolean On Error GoTo ErrFailed FindProcessId = -1 sExeName = UCase$(Trim$(sExeName)) 'Get the list of processes Do If lArraySize = 0 Then lArraySize = clInitNumProcesses Else lArraySize = lArraySize + clInitNumProcesses End If 'Size array to hold process IDs ReDim alProcIDs(lArraySize * 4) As Long 'Populate an array containing all process ID's lRet = EnumProcesses(alProcIDs(1), lArraySize * 4, lBytesReturned) 'Count number of processes returned lNumProcesses = lBytesReturned / 4 'Resize the array containing all the processes ReDim Preserve alProcIDs(lNumProcesses) 'Resize the array to contain all the matching processes ReDim alMatchingProcessIDs(1 To lNumProcesses) Loop While lArraySize <= lBytesReturned For lThisProcess = 1 To lNumProcesses 'Open the process lHwndProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, alProcIDs(lThisProcess)) If lHwndProcess <> 0 Then 'Get an array of the module handles for the specified process lRet = EnumProcessModules(lHwndProcess, alModules(1), 200&, lBytesNeeded) If lRet <> 0 Then 'Get Process Path and Name lRet = GetModuleFileNameExA(lHwndProcess, alModules(1), sModuleName, MAX_PATH) sProcessNamePath = Trim$(UCase$(Left$(sModuleName, lRet))) 'Get the Process Name sProcessName = Mid$(sProcessNamePath, InStrRev(sProcessNamePath, "\") + 1) If sProcessName = sExeName Then 'Found a matching process ID If lStartFromPID = -1 Then 'Return the first matching Id FindProcessId = alProcIDs(lThisProcess) Else If bPastLastMatch Then 'Return the next matching process Id FindProcessId = alProcIDs(lThisProcess) End If If alProcIDs(lThisProcess) = lStartFromPID Then 'Start the search for the previous matching PID bPastLastMatch = True End If End If End If End If End If 'Close the handle to this process lRet = CloseHandle(lHwndProcess) If FindProcessId > -1 Then Exit For End If Next Exit Function ErrFailed: Debug.Print "Error in FindProcessId: " & Err.Description FindProcessId = -1 End Function 'Returns the process ID for Outlook Sub Test() Dim lPid As Long 'Return the process IDs of the explorers lPid = FindProcessId("Outlook.exe") If lPid > -1 Then MsgBox "Outlook is open and has a PID of " & lPid Else MsgBox "Outlook is NOT open." End If End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder