McMillan's Visual Basic Code - Code Snippets - Test whether an Application is running
Code Snippets
Home



Test whether an Application is running

This snippet will allow you to test whether a given executable is running on your system

Option Explicit

Private Const TH32CS_SNAPPROCESS As Long = 2
Private Const MAX_PATH As Long = 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, typProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal _
hSnapShot As Long, typProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)

Public Function AppIsRunning(ByVal AppName As String) As Boolean

   Dim Process As PROCESSENTRY32

   Dim hSnapShot As Long
   Dim r As Long

   Dim l As Integer

   AppName = LCase(AppName)

   hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
   If hSnapShot <> -1 Then
      Process.dwSize = Len(Process)
      r = Process32First(hSnapShot, Process)
      While r
         If LCase(Left(Process.szExeFile, InStr(1, _
         Process.szExeFile, Chr(0)) - 1)) = AppName Then
            AppIsRunning = True
            r = False
         End If
         r = Process32Next(hSnapShot, Process)
      Wend
      CloseHandle hSnapShot
   End If

End Function

© Copyright Notice

Unless otherwise stated, the code on this site is Copyright to Andrew McMillan. You may use this code in your projects (both commercial and non-commercial) but you are not permitted to republish this code in any form without the Author's prior consent.

The code on this site is supplied "as is" and no claims are made as to its soundness. The Author claims no responsibility for or liability from use of said source code.


Home