Here’s a simple technique (pulling it out of archive to help answer a thread in a discussion forum) to verify whether or not a program (actually a process) is currently running on a computer, local or remote (if the permissions have been establish to validate such information remotely), using a simple WMI query.
'---------------------------------------------------------------------------------------
' Procedure : WMI_IsProcessRunning
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Determine if a process or Program is running or not
' Returns: True/False
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sExeName : Name of the process to look for
' sHost : Host computer to query, omit for the local PC
'
' Usage:
' ~~~~~~
' WMI_IsProcessRunning("firefox.exe")
' WMI_IsProcessRunning("outlook.exe")
' WMI_IsProcessRunning("msaccess.exe", "HomePC01")
' WMI_IsProcessRunning("msaccess.exe", "192.168.100.12")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2010-Feb-03 Initial Release
' 2 2020-08-21 Renamed the Function
' Updated Proc Header
' Code updated
' Updated Error Handler
'---------------------------------------------------------------------------------------
Public Function WMI_IsProcessRunning(sProcessName As String, Optional sHost As String = ".") As Boolean
On Error GoTo Error_Handler
Dim oWMI As Object 'WMI object to query about the PC's OS
Dim sWMIQuery As String 'WMI Query
Dim oCols As Object
Dim oCol As Object
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHost & "\root\cimv2")
sWMIQuery = "SELECT * FROM Win32_Process WHERE Name = '" & sProcessName & "'"
Set oCols = oWMI.ExecQuery(sWMIQuery)
If oCols.Count <> 0 Then WMI_IsProcessRunning = True
Error_Handler_Exit:
On Error Resume Next
Set oCols = Nothing
Set oWMI = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: WMI_IsProcessRunning" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
There are some other great resources on the subject of WMI to further develop such code. I good starting point is MSDN, see: WMI Tasks: Processes
Howdy, Daniel. You web site has proven to be a blessing to me more than once over the years. Now that it appears MS is breathing new life into Access I am sure your site will continue to be a touchstone of the community. Thanks for doing it.
I have been using your WMI_IsProcessRunning snippet for a while with good results but recently it has been throwing this:
SELECT name FROM Win32_Process WHERE Name = ‘C:\Program Files (x86)\TEC-IT\TWedge3\Bin\twedge.exe’
Error Number: -2147217385
Error Source: WMI_IsProcessRunning
Error Description: Invalid query
Twedge.exe is a QR code scanner. I also tried this with Notepad and get the same error. It doesn’t matter if the app in question is running or not. I also tried it with “SELECT * (instead f Select Name)
I recently “upgraded” to Win 11 and I’m wondering if perhaps you had noticed any similar problems and, if so, what you might suggest to find out if a process or task is running.
Thanks, I appreciate your time and advice.
Kent in Kansas City
Sadly, I’ve never experienced such issues. That said, I know that Windows 11 seems to have caused various problems, so when you mention having upgraded this gets me very nervous!
My only thought would be to try and perform the exact same query but through VBA PowerShell. No guarantee that it will yield different results, but that would be what I’d be trying quickly.
Great work, Daniel. Yours is the ONLY solution which seems to work consistently.
Very much appreciated!
— Bing