Count number of times same EXE is running

How do I count number of times same EXE is running
on the same machine
Who is Participating?
TimCotteeConnect With a Mentor Commented:
Option Explicit

Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle
As Long, ByVal dwProcId As Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long,
ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule
As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule
As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As
Long) As Long
Public Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

Public Function GetProcesses(strProcess As String) As Long
  Dim lProcCount     As Long
  Dim lProcPoss      As Long
  Dim lNum           As Long
  Dim ProcessIDs()   As Long
  Dim cbNeeded2      As Long
  Dim NumElements2   As Long
  Dim arrModules(1 To 200) As Long
  Dim lRet           As Long
  Dim sModName       As String
  Dim hwndProcess    As Long
  Dim iCounter       As Long
  Const SIZE = 500
  Const PROCESS_VM_READ = 16
  'need to get the array containing the process id's for each process object
  lProcCount = 8
  lProcPoss = 96
  Do While lProcCount <= lProcPoss
      lProcCount = lProcCount * 2
      ReDim ProcessIDs(lProcCount / 4) As Long
      lRet = EnumProcesses(ProcessIDs(1), lProcCount, lProcPoss)
  lNum = lProcPoss / 4
  For iCounter = 1 To lNum
      'Get a handle to the Process
      hwndProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessIDs(iCounter))
      'Got a Process handle
      If hwndProcess <> 0 Then
          'Get an array of the module handles for the specified
          lRet = EnumProcessModules(hwndProcess, arrModules(1), 200, cbNeeded2)
          'If the Module Array is retrieved, Get the ModuleFileName
          If lRet <> 0 Then
              sModName = Space(255)
              lRet = GetModuleFileNameExA(hwndProcess, arrModules(1), sModName, SIZE)
              'Form1.lstProcesses.AddItem ProcessIDs(iCounter) & vbTab & Left(sModName, lRet)
              If UCase(Left(sModName, lRet)) = UCase(strProcess) Then
                  GetProcesses = GetProcesses + 1
              End If
          End If
      End If
      'Close the handle
      lRet = CloseHandle(hwndProcess)
End Sub

You can then call

Msgbox GetProcesses ("c:\myapplication\a.exe")

Will return the number of instances of the specified application.
If you're looking at how many times your own app is running, you can simply add to a registry entry whenever the app starts, and subtract whenever it ends.  Then to find out how many, simply query the reg entry.

And, of course, if you do this query on start-up, you can prevent the app from proceeding, thereby creating a sort of max-users limit.

The main problem is that if the app does not shut down properly, it will never subtract that one from the reg entry...but there are some workarounds for that.
Richie_SimonettiIT OperationsCommented:
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

How abt this, this is a simple logic i thought of:

In the Form_load event of the form write this:

Private Sub Form_Load()
  If App.PrevInstance = True Then
    'means there is only ONE instance of the exe which is running
    'therefore write to a txtfile with value as 1

      'there is already an instance which is running, therefore update the variable counter by 1 always as it is opened
   end if
End Sub

Will this help?

Richie_SimonettiIT OperationsCommented:
If you like to play with Registry, i have something...
ProgramVBAuthor Commented:
ok Richie listening.......
ProgramVBAuthor Commented:
also what are the work arounds if the app does not shut down properly?
Richie_SimonettiIT OperationsCommented:
OK. I am not code with me, i am at home and code is at Office. Tomorrow mourning...
Richie_SimonettiIT OperationsCommented:
here i am:

Sub Main()
If Command$ <> "" Then
' ****** HERE *********
' check if the program is your program. If so,
' then open your log file, read the contents, add
' one to the numeric value stored in it and save result
' back to file.
' example:

'Open "c:\apps\exelog.txt" For output As #1
'Print #1, Command$ & " " & CStr(Now)
'Close #1
Call Shell(Command$, vbNormalFocus)
End If
End Sub

Registry Changes

The registry changes are:

HKEY_CLASSES_ROOT\exefile\shell\open\command to: "C:\exewrap.exe" "%1" %*

What happens it that instead of running the program directly, Windows calls our program, which logs filename and time, and then calls the program.
Hi ProgramVB,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept TimCottee's comment(s) as an answer.

ProgramVB, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer
per recommendation

Community Support Moderator @Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.