Multiple Apps

CheckProcess

Ease of Use

Hard

Version tested with

2000, 2002 

Submitted by:

mark007

Description:

The code will return either 'True' or 'False' depending on whether a process of the given name is running. 

Discussion:

As with many API examples much of the credit has to be given to the creators of API Guide at http://www.mentalis.org The code takes a 'snapshot' of the current running processes and then iterates throught them until there are none left. If it finds a process matching the given name it returns True. 

Code:

instructions for use

			

Const TH32CS_SNAPHEAPLIST = &H1 Const TH32CS_SNAPPROCESS = &H2 Const TH32CS_SNAPTHREAD = &H4 Const TH32CS_SNAPMODULE = &H8 Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE) Const TH32CS_INHERIT = &H80000000 Const MAX_PATH As Integer = 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 Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long) Function CheckProcess(strEXEName As String) As Boolean Dim hSnapShot As Long, uProcess As PROCESSENTRY32, r As Long 'Takes a snapshot of the processes and the heaps, modules, and threads used by the processes hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&) 'set the length of our ProcessEntry-type uProcess.dwSize = Len(uProcess) 'Retrieve information about the first process encountered in our system snapshot r = Process32First(hSnapShot, uProcess) Do While r > 0 If LCase$(strEXEName) = _ LCase$(Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0))) _ Then CheckProcess = True 'Retrieve information about the next process recorded in our system snapshot r = Process32Next(hSnapShot, uProcess) Loop 'close our snapshot handle CloseHandle hSnapShot End Function

How to use:

  1. Place the function in a code module
  2. Call it passing the name of the exe you want to check for
  3. It will then return true if the process is found and false otherwise
 

Test the code:

  1. As Above
  2. Refer to the attachment for an example
 

Sample File:

CheckProcess.zip 8.76KB 

Approved by mdmackillop


This entry has been viewed 133 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express