Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

# of Instances of an Application Opened 1

Status
Not open for further replies.

kmcclung

Programmer
Nov 20, 2001
120
0
0
US
Is there a way to determine how many instances of an application are running on the workstation? I know there is the PrevInstance Property but that only tells you whether a previous instance of an application is already running. I need to know if 4 are currently running - limiting the user to 4 instances at a time.

Any suggestions?
 
Is this 4 instances per machine or per network?

If (as I assume) the machines are on a network you could run a small database app one one machine which each instance accesses at startup, adding one to a counter. In your close and cleanup code, access the database again and subtract one.
It's much easier if the program already accesses a common database of course!

If it's 4 per machine just use registry to store usage count, using SaveSetting and GetSetting (or the equivalent API), both options being well documented on this forum.
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
John, there are problems for the method.. it wont let u know whether y'r application has ended, when your application crashes, or the user hits ctr+alt+del to end your program.. dus it?

May be on the app start, u can search for the Window Captions of all running threads, and know how many of them match your apps title.. it may be a little tedious, but it shud work... also, it works only in a single machine.. in a network....???? let us think... All the Best
Praveen Menon
pcmin@rediffmail.com
 
Thanks to both of you. I need this per machine. And although it may be tedious, PraveenMenon I think I'll use your solution.

Thanks for the help!! I'll let you know the result when I get the code in place.
 
I used the following code to determine if the title of the application (pulled from an ini file) is running. If so, I increment a counter and pop a message box if a match is found for a 5th time. This prevents the user from opening more than 4 instances of the same application....

Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long

Public sAppTitle As String 'Equal to the app's title

Dim bAppOpen As Boolean
Dim hWnd As Long
Dim sTitle As String
Dim i As Integer

i = 1

' Get first top-level window
hWnd = GetWindow(GetDesktopWindow(), GW_CHILD)
' Iterate through remaining windows
Do While hWnd <> hNull
sTitle = WindowTextLineFromWnd(hWnd)
If sTitle = sAppTitle Then
i = i + 1
If i > 4 Then
bAppOpen = True
Exit Do
End If
End If
' Get next child
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop

If bAppOpen = True Then
MsgBox &quot;The limit of 4 simultaneous instances has been reached&quot;, vbCritical
End
End If



Function WindowTextFromWnd(ByVal hWnd As Long) As String
Dim c As Long, s As String
c = GetWindowTextLength(hWnd)
' Some windows return huge length--ignore 0 <= c < 4096
If c And &HFFFFF000 Then Exit Function
s = String$(c, 0)
c = GetWindowText(hWnd, s, c + 1)
WindowTextFromWnd = s
End Function
'
Function WindowTextLineFromWnd(ByVal hWnd As Long) As String
Dim sTitle As String, cTitle As Long
sTitle = WindowTextFromWnd(hWnd)
' Chop off end of multiline captions
cTitle = InStr(sTitle, sCr)
WindowTextLineFromWnd = IIf(cTitle, Left$(sTitle, cTitle), sTitle)
End Function
 
There are not many who lets us know when their problem is solved... u stand tall among them kmcclung... All the Best
Praveen Menon
pcmin@rediffmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top