Attribute VB_Name = "modCloseTest"
Option Explicit
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long

Private Const WM_GETTEXT = &HD
Private Const WM_QUIT = &H12

Public strActiveTester As String
Private aCaptions() As String
Private lCount As Long

'//WIN32API Constant
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As String) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long) As Long
'Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

'//WIN32API Constant
Private Const WM_SYSCOMMAND = &H112
Private Const SC_CLOSE = &HF060&
Private Const SW_SHOW = &H5

Function CloseExtApp(ByVal lpWindowName As String) As Boolean
    Dim ehWnd, retVal As Long
    
    ehWnd = FindWindow(0&, lpWindowName)
    If ehWnd <> 0 Then
        retVal = ShowWindow(ehWnd, SW_SHOW)
        SendMessage ehWnd, WM_SYSCOMMAND, SC_CLOSE, 0&
        DestroyWindow ehWnd
        
        CloseExtApp = True
    Else
        CloseExtApp = False
    End If
End Function

Public Function GetAllCaptions() As Variant
    lCount = 0
    Call EnumWindows(AddressOf EnumWindowsProc, 0&)
    If lCount Then ReDim Preserve aCaptions(lCount - 1)
    GetAllCaptions = IIf(lCount, aCaptions, Array())
End Function

Private Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
    Dim sBuffer As String * 260
    If IsWindowVisible(hWnd) Then
        ReDim Preserve aCaptions(lCount)
        aCaptions(lCount) = Left(sBuffer, SendMessage(hWnd, WM_GETTEXT, 260, ByVal sBuffer))
        If Len(Trim(aCaptions(lCount))) Then lCount = lCount + 1
    End If
    EnumWindowsProc = hWnd
End Function

Public Function MakeStrArray(vCaps As Variant, iArraySize As Integer) As Variant
Dim strAllRunningApps(50) As String
Dim iCount As Integer

    For iCount = 0 To iArraySize
        strAllRunningApps(iCount) = vCaps(iCount)
    Next

    MakeStrArray = strAllRunningApps

End Function
' if caption = " - Tester"
Function TesterIsRunning(strArray As Variant, iArraySize As Integer) As Boolean
Dim iCounter As Integer
Dim strCompare As String

    For iCounter = 0 To iArraySize
        strCompare = Right(strArray(iCounter), 9)
        If strCompare = " - Tester" Then
            TesterIsRunning = True
            strActiveTester = strArray(iCounter)
            Exit Function
        End If
    Next
    
    TesterIsRunning = False
    
End Function
