You can use the Toolhelp API to get the process Id of any running process by its name.
___
[tt]
Option Explicit
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessId As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Const TH32CS_SNAPPROCESS = 2
Const MAX_PATH = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Function GetProcessId(Process As String) As Long
Dim hSnapShot As Long, pe32 As PROCESSENTRY32
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, ByVal 0)
pe32.dwSize = Len(pe32)
ProcessFirst hSnapShot, pe32
Do While ProcessNext(hSnapShot, pe32)
If InStr(1, pe32.szExeFile, Process & vbNullChar, vbTextCompare) = 1 Then
GetProcessId = pe32.th32ProcessID
Exit Do
End If
Loop
CloseHandle hSnapShot
End Function
Private Sub Form_Load()
MsgBox GetProcessId("EXPLORER.EXE")
End
End Sub[/tt]
___
By the way, did you read FAQ222-2244? Please do it now if you haven't yet. Its really worth reading.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.