Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine Explorer ProcessID

Status
Not open for further replies.

windoz255

Technical User
Apr 8, 2003
17
GB
Hi,

Can anyone assist me with finding the ProcessID for "explorer.exe". ? (I am wanting to know the PID for this single executable and nothing else.)

Any sample code would be useful.

Thanks,
Al.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top