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 "The limit of 4 simultaneous instances has been reached", 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