?
Solved

Count number of times same EXE is running

Posted on 2002-07-05
11
Medium Priority
?
155 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
[X]
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
11 Comments
 
LVL 43

Accepted Solution

by:
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_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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

649 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