Option Explicit

   Declare Function Init_MatrixAPI Lib "matrix32.dll" () As Integer
   
   Declare Function Release_MatrixAPI Lib "matrix32.dll" () As Integer
   
   Declare Function GetVersionAPI Lib "matrix32.dll" () As Long

   Declare Function GetVersionDRV Lib "matrix32.dll" () As Long

   Declare Function GetVersionDRV_USB Lib "matrix32.dll" () As Long

   Declare Function PausePrinterActivity Lib "matrix32.dll" () As Integer
   
   Declare Function ResumePrinterActivity Lib "matrix32.dll" () As Integer
   
   Declare Function GetPortAdr Lib "matrix32.dll" (ByVal DNG_Port As Integer) As Integer
   
   Declare Function Dongle_Find Lib "matrix32.dll" () As Integer
   
   Declare Function Dongle_Count Lib "matrix32.dll" (ByVal DNG_Port As Integer) As Integer

   Declare Function Dongle_Version Lib "matrix32.dll" (ByVal DNG_Nummer As Integer, _
                                                       ByVal DNG_Port As Integer) As Long

   Declare Function Dongle_Model Lib "matrix32.dll" (ByVal DNG_Nummer As Integer, _
                                                     ByVal DNG_Port As Integer) As Integer

   Declare Function Dongle_MemSize Lib "matrix32.dll" (ByVal DNG_Nummer As Integer, _
                                                       ByVal DNG_Port As Integer) As Integer

   Declare Function Dongle_ReadData Lib "matrix32.dll" (ByVal UserCode As Long, _
                                                        ByRef DataIn As Long, _
                                                        ByVal MaxVar As Integer, _
                                                        ByVal DNG_Nummer As Integer, _
                                                        ByVal DNG_Port As Integer) As Integer

   Declare Function Dongle_ReadDataEx Lib "matrix32.dll" (ByVal UserCode As Long, _
                                                          ByRef DataIn As Long, _
                                                          ByVal Fpos As Integer, _
                                                          ByVal MaxVar As Integer, _
                                                          ByVal DNG_Nummer As Integer, _
                                                          ByVal DNG_Port As Integer) As Integer

   Declare Function Dongle_WriteData Lib "matrix32.dll" (ByVal UserCode As Long, _
                                                         ByRef DataOut As Long, _
                                                         ByVal MaxVar As Integer, _
                                                         ByVal DNG_Nummer As Integer, _
                                                         ByVal DNG_Port As Integer) As Integer

   Declare Function Dongle_WriteDataEx Lib "matrix32.dll" (ByVal UserCode As Long, _
                                                           ByRef DataOut As Long, _
                                                           ByVal Fpos As Integer, _
                                                           ByVal MaxVar As Integer, _
                                                           ByVal DNG_Nummer As Integer, _
                                                           ByVal DNG_Port As Integer) As Integer

   Declare Function Dongle_WriteKey Lib "matrix32.dll" (ByVal UserCode As Long, _
                                                        ByRef KeyData As Long, _
                                                        ByVal DNG_Nummer As Integer, _
                                                        ByVal DNG_Port As Integer) As Integer

   Declare Function Dongle_EncryptData Lib "matrix32.dll" (ByVal UserCode As Long, _
                                                           ByRef DataBlock As Long, _
                                                           ByVal DNG_Nummer As Integer, _
                                                           ByVal DNG_Port As Integer) As Integer

   Declare Function Dongle_DecryptData Lib "matrix32.dll" (ByVal UserCode As Long, _
                                                           ByRef DataBlock As Long, _
                                                           ByVal DNG_Nummer As Integer, _
                                                           ByVal DNG_Port As Integer) As Integer

   Declare Function SetConfig_MatrixNet Lib "matrix32.dll" (ByVal nAccess As Integer, _
                                                            ByVal nFile As String) As Integer

   Declare Function LogIn_MatrixNet Lib "matrix32.dll" (ByVal UserCode As Long, _
                                                        ByVal AppSlot As Integer, _
                                                        ByVal DNG_Nr As Integer) As Integer

   Declare Function LogOut_MatrixNet Lib "matrix32.dll" (ByVal UserCode As Long, _
                                                         ByVal AppSlot As Integer, _
                                                         ByVal DNG_Nr As Integer) As Integer

Private Sub Auto_Open()
  Dim ret As Integer
  
     Worksheets("Tabelle2").Visible = xlSheetVisible
     Worksheets("Tabelle1").Visible = xlSheetVeryHidden
  
     On Error GoTo End_Exit_Err  'Force exit if MATRIX32.DLL not found
                 
     ret = Check_Matrix            'Check if Matrix dongle is present
     If ret = 1 Then
        GoTo Ok_Run
     Else
        GoTo End_Exit
     End If

End_Exit_Err:
     MsgBox "MATRIX32.DLL not found !"
End_Exit:
     Application.DisplayAlerts = False
     Application.Quit
  
Ok_Run:
     Worksheets("Tabelle1").Visible = xlSheetVisible
     Worksheets("Tabelle2").Visible = xlSheetVeryHidden

End Sub

Function Check_Matrix() As Integer
  Dim DNG_Port    As Integer
  Dim DNG_Nr      As Integer
  Dim DNG_Found   As Integer
  Dim DataIn(256) As Long
  Dim i           As Integer
  Dim RetCode     As Integer
  
  Const UserCode = 1234    '*** Demo UserCode, change to your own UserCode
  
  RetCode = Init_MatrixAPI()

  DNG_Found = 0

  For i = 0 To 3
      If i = 0 Then
         DNG_Port = 85   '*** 85 (ASCII 'U') = USB
      Else
         DNG_Port = i    '*** 1-3 = LPT1-LPT3
      End If
         
      DNG_Nr = Dongle_Count(DNG_Port)
      If DNG_Nr > 0 Then
         RetCode = Dongle_ReadData(UserCode, DataIn(0), 3, DNG_Nr, DNG_Port)
         If RetCode > 0 Then
            DNG_Found = 1
            Exit For     '*** Matrix device found
         End If
      End If
  Next i

  RetCode = Release_MatrixAPI()
  
  If DNG_Found <= 0 Then
     MsgBox "Matrix device not found !"
  End If
  
  Check_Matrix = DNG_Found
End Function