modVBAuthTesting.bas v2

' ============================================================
' Copyright (c) 2025-2026 Peter Bryant (Corylus Business Systems)
'                    and  Andrew Richards (The IT Service Ltd)
' Part of VBAuth - https://vbauth.com
' Licensed under the MIT Licence - see LICENCE.txt
' This copyright notice must not be removed or altered.
' Module version 2, for VBAuth 1.0.0.3
' ============================================================

' ============================================================
' modVBAuthTesting - diagnostic routines for VBAuth.dll
' ============================================================
' run these in order when installing VBAuth for the first time,
' or when something has stopped working and it is not yet clear
' whether the fault is the DLL, the licence or the tenant
'
'   test 1 - Test_CheckDependencies             are the required DLLs present
'   test 2 - Test_CheckPresenceOfLicenceFiles   do the .LIC files exist on disk
'   test 3 - Test_CheckLicence                  are those files valid for this tenant
'
' each test has a Test_ wrapper that shows the answer in a
' message box, and a worker function that returns it in words
' so a calling form can present it however it likes
' ============================================================

Option Compare Database
Option Explicit

' ============================================================
' GetVBAuthFolder
' asks the loaded DLL where it lives, rather than reading the
' registry or depending on WScript, so the answer is always the
' copy actually in use
' returns an empty string if the DLL cannot be instantiated, and
' raises to the caller's handler if COM itself refuses
' ============================================================
Public Function GetVBAuthFolder() As String
Dim objAuth As Object   ' the DLL, released as soon as it has answered

    'instantiating it is the only reliable way to learn where the runtime loaded it from
    Set objAuth = CreateObject("VBAuth.GraphAuthenticator")
    GetVBAuthFolder = objAuth.DllLocation
    'release the DLL
    Set objAuth = Nothing

End Function

' ============================================================
' Test_CheckDependencies
' shows the dependency check result in a message box
' run it from the Immediate Window: Test_CheckDependencies
' ============================================================
Public Sub Test_CheckDependencies()
Dim strMessage As String    ' the worker's answer in words, local and shadowing the global

    'the icon is the only difference - the text is whatever the worker produced
    If CheckDependencies(strMessage) Then
        MsgBox strMessage, vbInformation, "VBAuth Test"
    Else
        MsgBox strMessage, vbCritical, "VBAuth Test"
    End If

End Sub

' ============================================================
' Test_CheckPresenceOfLicenceFiles
' shows the licence file check result in a message box
' run it from the Immediate Window: Test_CheckPresenceOfLicenceFiles
' ============================================================
Public Sub Test_CheckPresenceOfLicenceFiles()
Dim strMessage As String    ' the worker's answer in words, local and shadowing the global

    If CheckPresenceOfLicenceFiles(strMessage) Then
        MsgBox strMessage, vbInformation, "VBAuth Test"
    Else
        MsgBox strMessage, vbCritical, "VBAuth Test"
    End If

End Sub

' ============================================================
' Test_CheckLicence
' shows the licence validation result in a message box
' run it from the Immediate Window: Test_CheckLicence
' ============================================================
Public Sub Test_CheckLicence()
Dim strMessage As String    ' the worker's answer in words, local and shadowing the global

    If CheckLicence(strMessage) Then
        MsgBox strMessage, vbInformation, "VBAuth Test"
    Else
        MsgBox strMessage, vbCritical, "VBAuth Test"
    End If

End Sub

' ============================================================
' CheckDependencies
' asks the DLL which of its own supporting assemblies it can
' find, and turns the JSON answer into something a user can read
' returns False with strMessage naming what is missing and what
' version was expected
'
' the DLL can return three kinds of row - a normal entry, a "---"
' separator used to group the list, and a status that is neither
' OK nor a hard failure.  all three are handled, because a
' separator printed as if it were a DLL name reads as a fault
' ============================================================
Public Function CheckDependencies(ByRef strMessage As String) As Boolean
Dim objAuth         As Object       ' the DLL, released on every path including the error path
Dim strResult       As String       ' raw JSON from the DLL
Dim objJson         As Object       ' parsed JSON, released with the procedure
Dim objDllArray     As Collection   ' the rows, when the check failed
Dim objDll          As Object       ' the failure row being formatted
Dim strErrors       As String       ' failure lines, listed first so they are seen
Dim strOk           As String       ' passing lines, listed after the failures
Dim objDllArrayOk   As Collection   ' the rows, when the check passed
Dim objDllOk        As Object       ' the passing row being formatted

    'assume failure, so a caller that ignores the return value is not misled
    CheckDependencies = False
    strMessage = ""

    On Error GoTo ErrHandler

    Set objAuth = CreateObject("VBAuth.GraphAuthenticator")
    strResult = objAuth.CheckDependenciesForVBA()
    Set objJson = JSONConverter.ParseJson(strResult)

    If objJson("success") = True Then
        Set objDllArrayOk = objJson("data")
        strMessage = "Dependencies check PASSED" & vbCrLf & vbCrLf

        For Each objDllOk In objDllArrayOk
            Select Case objDllOk("status")
                Case "---"
                    'a separator, so break the list rather than printing a nameless entry
                    strMessage = strMessage & vbCrLf
                Case "OK"
                    strMessage = strMessage & objDllOk("name")
                    'a row with a required version reports the version found against it
                    If objDllOk("required") <> "" Then
                        strMessage = strMessage & " v" & objDllOk("found")
                    'a row with no required version may still have something worth showing
                    ElseIf objDllOk("found") <> "" Then
                        strMessage = strMessage & " - " & objDllOk("found")
                    End If
                    strMessage = strMessage & vbCrLf
                Case Else
                    'anything else is a warning worth showing, not a failure worth stopping for
                    strMessage = strMessage & objDllOk("status") & " - " & objDllOk("name") & vbCrLf
            End Select
        Next objDllOk

        CheckDependencies = True
    Else
        Set objDllArray = objJson("data")
        strErrors = ""
        strOk = ""

        For Each objDll In objDllArray
            Select Case objDll("status")
                Case "---"
                    'separators carry no meaning once the list is split into two groups
                Case "OK"
                    strOk = strOk & objDll("status") & " - " & objDll("name") & _
                            " v" & objDll("found") & vbCrLf
                Case Else
                    'name the version expected, since that is what the user has to go and find
                    strErrors = strErrors & objDll("status") & " - " & objDll("name") & _
                                " (required: " & objDll("required") & ")" & vbCrLf
            End Select
        Next objDll

        'failures first - the passing list is context, not the answer
        strMessage = "Dependencies check FAILED" & vbCrLf & vbCrLf & strErrors & strOk
    End If

    'children before parents - both arrays point inside objJson, and only one of them
    'was ever set, but releasing Nothing is harmless so the full set goes
    'release the failure-branch array
    Set objDllArray = Nothing
    'release the success-branch array
    Set objDllArrayOk = Nothing
    'release the parsed JSON
    Set objJson = Nothing
    'release the DLL
    Set objAuth = Nothing
    Exit Function

ErrHandler:
    'the number is included because a COM failure here is usually a registration problem
    'and the number is what identifies which
    strMessage = "Dependencies check ERROR: " & Err.Number & " - " & Err.Description

    'the same cleanup as the success path, duplicated rather than jumped to
    'children before parents - both arrays point inside objJson, and only one of them
    'was ever set, but releasing Nothing is harmless so the full set goes
    'release the failure-branch array
    Set objDllArray = Nothing
    'release the success-branch array
    Set objDllArrayOk = Nothing
    'release the parsed JSON
    Set objJson = Nothing
    'release the DLL
    Set objAuth = Nothing

End Function

' ============================================================
' CheckPresenceOfLicenceFiles
' looks for .LIC files beside the DLL, which is the first thing
' to establish before asking whether a licence is valid
' returns False with strMessage explaining whether the folder
' could not be found or simply held no licence files
' ============================================================
Public Function CheckPresenceOfLicenceFiles(ByRef strMessage As String) As Boolean
Dim strFolder   As String       ' where the DLL reports it was loaded from
Dim strFile     As String       ' the file name Dir is currently returning
Dim colFiles    As New Collection   ' licence file names found, auto-instantiated, no disposal
Dim varFile     As Variant      ' the collection entry being listed

    'assume failure until files have actually been found
    CheckPresenceOfLicenceFiles = False
    strMessage = ""

    On Error GoTo ErrHandler

    strFolder = GetVBAuthFolder()

    If strFolder = "" Then
        'GetVBAuthFolder could not instantiate the DLL, so there is nowhere to look
        strMessage = "Could not determine VBAuth deployment folder." & vbCrLf & vbCrLf & _
                     "Ensure VBAuth.dll is correctly registered."
        Exit Function
    End If

    'Dir is used rather than the FileSystemObject so nothing outside VBA is required
    strFile = Dir(strFolder & "\*.LIC")
    Do While strFile <> ""
        colFiles.Add strFile
        strFile = Dir()
    Loop

    If colFiles.Count = 0 Then
        'the folder is right but unlicensed, which is a purchase question not a fault
        strMessage = "No licence files found in:" & vbCrLf & strFolder & vbCrLf & vbCrLf & _
                     "Please visit https://vbauth.com to purchase a licence."
        Exit Function
    End If

    strMessage = colFiles.Count & " licence file(s) found in:" & vbCrLf & _
                 strFolder & vbCrLf & vbCrLf

    'name every file, because the file name is what has to match the tenant
    For Each varFile In colFiles
        strMessage = strMessage & varFile & vbCrLf
    Next varFile

    If colFiles.Count = 1 Then
        'with one file there is nothing to compare it against, so say what it should be called
        strMessage = strMessage & vbCrLf & _
                     "Note: filename should match your Microsoft 365 tenant ID."
    End If

    CheckPresenceOfLicenceFiles = True
    Exit Function

ErrHandler:
    strMessage = "Licence file check ERROR: " & Err.Number & " - " & Err.Description

End Function

' ============================================================
' CheckLicence
' asks the DLL whether the licence files on disk are valid for
' this tenant, covering both the developer and client tenant IDs
' returns False with strMessage listing each file and why it was
' rejected, so a wrong-tenant licence is obvious rather than just
' being reported as invalid
' ============================================================
Public Function CheckLicence(ByRef strMessage As String) As Boolean
Dim objAuth     As Object       ' the DLL, released on every path including the error path
Dim strResult   As String       ' raw JSON from the DLL
Dim objJson     As Object       ' parsed JSON, released with the procedure
Dim objLicArray As Collection   ' one row per licence file examined
Dim objLic      As Object       ' the licence row being formatted
Dim strErrors   As String       ' rejected files and the reason each was rejected
Dim strValid    As String       ' accepted files and their expiry dates
Dim strTenantID As String       ' the tenant the licence has to be valid for

    'assume failure until the DLL says otherwise
    CheckLicence = False
    strMessage = ""

    On Error GoTo ErrHandler

    'AppID 1 always exists, so it is the safest row to read the tenant from
    strTenantID = Nz(DLookup("TenantID", "lclVBAuthAdmin", "AppID = 1"), "")

    If strTenantID = "" Then
        'without a tenant there is nothing to validate against, and setup has not been done
        strMessage = "No Tenant ID found in setup table." & vbCrLf & vbCrLf & _
                     "Please complete the Entra app setup step first."
        Exit Function
    End If

    Set objAuth = CreateObject("VBAuth.GraphAuthenticator")
    strResult = objAuth.CheckLicence(strTenantID)
    Set objJson = JSONConverter.ParseJson(strResult)
    Set objLicArray = objJson("data")

    strErrors = ""
    strValid = ""

    'sort the rows into two lists first, then decide which order to show them in
    For Each objLic In objLicArray
        If objLic("status") = "Fully Valid" Then
            'the expiry date matters even on a passing licence, so it is shown
            strValid = strValid & objLic("fileName") & " - " & objLic("status") & _
                       ", Expires: " & objLic("expiresAt") & vbCrLf
        Else
            strErrors = strErrors & objLic("fileName") & " - " & objLic("status") & vbCrLf
        End If
    Next objLic

    If objJson("success") = True Then
        'on success the valid files lead, and any rejected ones follow as context
        strMessage = "Licence check PASSED" & vbCrLf & vbCrLf & strValid & strErrors
        CheckLicence = True
    Else
        'on failure the reverse, so the reason is the first thing read
        strMessage = "Licence check FAILED" & vbCrLf & vbCrLf & strErrors & strValid
    End If

    'children before parents - objLicArray points inside objJson
    'release the licence array
    Set objLicArray = Nothing
    'release the parsed JSON
    Set objJson = Nothing
    'release the DLL
    Set objAuth = Nothing
    Exit Function

ErrHandler:
    strMessage = "Licence check ERROR: " & Err.Number & " - " & Err.Description

    'the same cleanup as the success path, duplicated rather than jumped to
    'children before parents - objLicArray points inside objJson
    'release the licence array
    Set objLicArray = Nothing
    'release the parsed JSON
    Set objJson = Nothing
    'release the DLL
    Set objAuth = Nothing

End FunctionCode language: PHP (php)
Updated on September 11, 2026