Public Const MAX_PATH As Integer = 260
'** OpenHandle Constants.
Public Const PROCESS_QUERY_INFORMATION = 1024
Public Const PROCESS_VM_READ = 16
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SYNCHRONIZE = &H100000
'** API declarations.
Public Declare Sub CloseHandle Lib "kernel32.dll" (ByVal hPass As Long)
Public Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleBaseNameA Lib "psapi.dll" (ByVal lngProcessHandle As Long, ByVal lngModuleHandle As Long, ByVal strModuleName As String, ByVal lngSize As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Function GetProcessHandle(strName As String) As Long
'This Function accepts the name of the EXE (i.e. SomeProgram.exe) and scans
'the process list for it. When it is found, the Process ID is returned. If
'it is not found, then the function returns 0.
Dim lngProcessID() As Long
Dim lngCB As Long
Dim lngCBNeeded As Long
Dim lngRetCode As Long
Dim lngLoop As Long
Dim lngProcessHandle As Long
Dim strModuleName As String
Dim strTemp() As String
Dim lngModules(1 To 200) As Long
Dim lngCbNeeded2 As Long
Dim intTemp As Integer
'** Get the current list of running Processes IDs
'******************************************************************************
lngCB = 8
lngCBNeeded = 96
Do While lngCB <= lngCBNeeded
lngCB = lngCB * 2
ReDim lngProcessIDs(lngCB / 4) As Long
lngRetCode = EnumProcesses(lngProcessIDs(1), lngCB, lngCBNeeded)
If lngRetCode = 0 Then
MsgBox Err.LastDllError
Exit Do
End If
Loop
'******************************************************************************
'** Get the name of the Base Module in each Process Space.
'** NOTE : If you wanted to, you could also use the GetModuleBaseNameA API
'** to get every DLL and OCX loaded in the process space.
'******************************************************************************
For lngLoop = 1 To lngCBNeeded / 4
'Get a handle to the Process
lngProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION _
Or PROCESS_VM_READ, 0, lngProcessIDs(lngLoop))
'Got a Process handle
If lngProcessHandle <> 0 Then
'Get an array of the module handles for the specified
'process
lngRetCode = EnumProcessModules(lngProcessHandle, lngModules(1), 200, lngCbNeeded2)
'If the Module Array is retrieved, Get the ModuleFileName
If lngRetCode <> 0 Then
strModuleName = Space(MAX_PATH)
lngRetCode = GetModuleBaseNameA(lngProcessHandle, lngModules(1), strModuleName, 500)
ReDim Preserve strTemp(intTemp)
strTemp(intTemp) = Trim$(strModuleName)
'** Check to see if the current process is the target process.
If UCase$(Trim$(strModuleName)) = UCase$(Trim$(strName & Chr$(0))) Then
GetProcessHandle = lngProcessIDs(lngLoop)
Exit For
End If
intTemp = intTemp + 1
End If
End If
Next lngLoop
'******************************************************************************
End Function