Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Count number of times same EXE is running

Posted on 2002-07-05
11
151 Views
Last Modified: 2010-05-02
How do I count number of times same EXE is running
on the same machine
0
Comment
Question by:ProgramVB
11 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 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_QUERY_INFORMATION = 1024
  Const PROCESS_VM_READ = 16
  Const PROCESS_ALL_ACCESS = &H1F0FFF
 
  '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)
  Loop
  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
          'process
          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)
  Next
 
End Sub

You can then call

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

Will return the number of instances of the specified application.
0
 
LVL 22

Expert Comment

by:rspahitz
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7132959
Listening...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 2

Expert Comment

by:priya_pbk
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

   else
      '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?

-priya
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7138385
If you like to play with Registry, i have something...
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 7140107
ok Richie listening.......
0
 
LVL 1

Author Comment

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

Expert Comment

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

Expert Comment

by:Richie_Simonetti
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
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.
0
 
LVL 49

Expert Comment

by:DanRollins
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
0
 

Expert Comment

by:SpideyMod
ID: 8053106
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

839 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