Count number of times same EXE is running

Posted on 2002-07-05
Medium Priority
Last Modified: 2010-05-02
How do I count number of times same EXE is running
on the same machine
Question by:ProgramVB
LVL 43

Accepted Solution

TimCottee earned 200 total points
ID: 7132276
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.
LVL 22

Expert Comment

ID: 7132483
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.
LVL 16

Expert Comment

ID: 7132959
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 7133479
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?

LVL 16

Expert Comment

ID: 7138385
If you like to play with Registry, i have something...

Author Comment

ID: 7140107
ok Richie listening.......

Author Comment

ID: 7140111
also what are the work arounds if the app does not shut down properly?
LVL 16

Expert Comment

ID: 7142041
OK. I am not code with me, i am at home and code is at Office. Tomorrow mourning...
LVL 16

Expert Comment

ID: 7143435
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.
LVL 49

Expert Comment

ID: 7990725
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

Expert Comment

ID: 8053106
per recommendation

Community Support Moderator @Experts Exchange

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question