Solved

Count number of times same EXE is running

Posted on 2002-07-05
11
148 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
Comment Utility
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
Comment Utility
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
Comment Utility
Listening...
0
 
LVL 2

Expert Comment

by:priya_pbk
Comment Utility
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
Comment Utility
If you like to play with Registry, i have something...
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:ProgramVB
Comment Utility
ok Richie listening.......
0
 
LVL 1

Author Comment

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

Expert Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now