Option Strict Off
Option Explicit On
Module Matrix
	'******************************************************************
	'* MATRIX.VB
	'* Example for VisualBasic.NET
	'*
	'* Copyright (C) TDi GmbH
	'******************************************************************
	
	
	Structure DNGINFO
		Dim LPT_Nr As Short
		Dim LPT_Adr As Short
		Dim DNG_Cnt As Short
	End Structure
	
	Dim RetCode As Short
	Dim xBuffer(3) As DNGINFO
	Dim DataIn(256) As Integer
	Dim DataOut(256) As Integer
	Dim KeyData(4) As Integer
	Dim DataBlock(2) As Integer
	Dim DNG_LPTADR As Short
	Dim DNG_Count As Short
	Dim DNG_Nummer As Short
	Dim DNG_Mem As Short
	Dim DNG_MaxVar As Short
	Dim DNG_Version As Integer
	Dim DNG_SerNr As Integer
	Dim VerMajor As Short
	Dim VerMinor As Short
	Dim AppSlot As Short
	Const Shift16 As Integer = 2 ^ 16
	
	'***********************************************************
	'* In this example we use the demo UserCode 1234 and the   *
	'* LPT-Port 1.                                             *
	'* - Set DNG_Port = 85, if you like to test the USB dongle *
	'***********************************************************
    Const UserCode As Short = 1234
	Dim DNG_Port As Short
	
	
	Declare Function Init_MatrixAPI Lib "matrix32.dll" () As Short
	
	Declare Function Release_MatrixAPI Lib "matrix32.dll" () As Short
	
	Declare Function GetVersionAPI Lib "matrix32.dll" () As Integer
	
	Declare Function GetVersionDRV Lib "matrix32.dll" () As Integer
	
	Declare Function GetVersionDRV_USB Lib "matrix32.dll" () As Integer
	
	Declare Function PausePrinterActivity Lib "matrix32.dll" () As Short
	
	Declare Function ResumePrinterActivity Lib "matrix32.dll" () As Short
	
	Declare Function GetPortAdr Lib "matrix32.dll" (ByVal DNG_Port As Short) As Short
	
	Declare Function Dongle_Find Lib "matrix32.dll" () As Short
	
	'UPGRADE_WARNING: FE die Struktur DNGINFO mEsen Marshalling-Attribute möglicherweise als ein Argument in dieser Declare-Anweisung weitergegeben werden. Klicken Sie hier fE weitere Informationen: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"'
	Declare Function Dongle_FindEx Lib "matrix32.dll" (ByRef xBuffer As DNGINFO) As Short
	
	Declare Function Dongle_Count Lib "matrix32.dll" (ByVal DNG_Port As Short) As Short
	
	Declare Function Dongle_Version Lib "matrix32.dll" (ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Integer
	
	Declare Function Dongle_Model Lib "matrix32.dll" (ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short
	
	Declare Function Dongle_MemSize Lib "matrix32.dll" (ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short
	
	Declare Function Dongle_ReadData Lib "matrix32.dll" (ByVal UserCode As Integer, ByRef DataIn As Integer, ByVal MaxVar As Short, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short
	
	Declare Function Dongle_ReadDataEx Lib "matrix32.dll" (ByVal UserCode As Integer, ByRef DataIn As Integer, ByVal Fpos As Short, ByVal MaxVar As Short, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short
	
	Declare Function Dongle_WriteData Lib "matrix32.dll" (ByVal UserCode As Integer, ByRef DataOut As Integer, ByVal MaxVar As Short, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short
	
	Declare Function Dongle_WriteDataEx Lib "matrix32.dll" (ByVal UserCode As Integer, ByRef DataOut As Integer, ByVal Fpos As Short, ByVal MaxVar As Short, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short
	
	Declare Function Dongle_WriteKey Lib "matrix32.dll" (ByVal UserCode As Integer, ByRef KeyData As Integer, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short
	
	Declare Function Dongle_ReadSerNr Lib "matrix32.dll" (ByVal UserCode As Integer, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Integer
	
	Declare Function Dongle_EncryptData Lib "matrix32.dll" (ByVal UserCode As Integer, ByRef DataBlock As Integer, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short
	
	Declare Function Dongle_DecryptData Lib "matrix32.dll" (ByVal UserCode As Integer, ByRef DataBlock As Integer, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short
	
	Declare Function SetConfig_MatrixNet Lib "matrix32.dll" (ByVal nAccess As Short, ByVal nFile As String) As Short
	
	Declare Function LogIn_MatrixNet Lib "matrix32.dll" (ByVal UserCode As Integer, ByVal AppSlot As Short, ByVal DNG_Nr As Short) As Short
	
	Declare Function LogOut_MatrixNet Lib "matrix32.dll" (ByVal UserCode As Integer, ByVal AppSlot As Short, ByVal DNG_Nr As Short) As Short
	
	
	
	Public Sub SetActivePort(ByRef Port As Object)
		DNG_Port = 0
		
		Select Case Port
			Case "LPT1"
				DNG_Port = 1
			Case "LPT2"
				DNG_Port = 2
			Case "LPT3"
				DNG_Port = 3
			Case "USB"
				DNG_Port = 85
		End Select
		
	End Sub
	
	
	Public Sub LPT_Port_Exists()
		
		If DNG_Port = 85 Then '* USB *
			MsgBox("DNG_Port=85 (USB)" & Chr(13) & "This function works only with LPT-Ports!")
			Exit Sub
		End If
		
		RetCode = Init_MatrixAPI()
		
		'***********************************************************
		'* Check whether the LPT port DNG_Port is available        *
		'***********************************************************
		DNG_LPTADR = GetPortAdr(DNG_Port)
		
		If DNG_LPTADR = 0 Then
			MsgBox("LPT" & DNG_Port & " not available!")
		Else
			MsgBox("The Address of LPT" & DNG_Port & " is: " & "0x" & Hex(DNG_LPTADR))
		End If
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub Number_of_Dongles()
		
		RetCode = Init_MatrixAPI()
		
		'***********************************************************
		'* Search for number of Dongles at DNG_Port                *
		'***********************************************************
		DNG_Count = Dongle_Count(DNG_Port)
		
		If DNG_Count <= 0 Then
			MsgBox("There is no Dongle at Port " & DNG_Port & " available!")
		Else
			MsgBox("Found: " & DNG_Count & " Dongles at Port " & DNG_Port)
		End If
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub Dongle_Find_Ex()
		Dim i As Object
		
		Dim MessageString As String
		
		RetCode = Init_MatrixAPI()
		
		'***********************************************************
		'* Search all LPT-Dongles                                  *
		'***********************************************************
		
		RetCode = Dongle_FindEx(xBuffer(0))
		
		If RetCode <= 0 Then
			MsgBox("No LPT Ports available!")
			RetCode = Release_MatrixAPI()
			Exit Sub
		Else
			MessageString = "Found: " & RetCode & " LPT-Ports" & Chr(13) & Chr(13)
			
			For i = 1 To RetCode
				'UPGRADE_WARNING: Die Standardeigenschaft des Objekts i konnte nicht aufgelöst werden. Klicken Sie hier fE weitere Informationen: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"'
				MessageString = MessageString & "Address of LPT" & i & ": " & "0x" & Hex(xBuffer(i - 1).LPT_Adr) & Chr(13) & "Dongles at LPT" & i & "  : " & xBuffer(i - 1).DNG_Cnt & Chr(13) & Chr(13)
			Next i
		End If
		
		MsgBox(MessageString)
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub Memory_of_Dongle()
		
		RetCode = Init_MatrixAPI()
		
		'***********************************************************
		'* Read memory size of last Dongle at DNG_Port and         *
		'* calculate the maximum number of data fields             *
		'***********************************************************
		DNG_Nummer = Dongle_Count(DNG_Port)
		
		DNG_Mem = Dongle_MemSize(DNG_Nummer, DNG_Port)
		
		If DNG_Mem <= 0 Then
			MsgBox("Error while determining Dongle memory!")
			Exit Sub
		End If
		
		DNG_MaxVar = DNG_Mem / 4
		
		MsgBox("Number of variables of this Dongle: " & DNG_MaxVar)
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub Version_of_Dongle()
		
		RetCode = Init_MatrixAPI()
		
		'***********************************************************
		'* Read Dongle version from last Dongle at DNG_Port        *
		'***********************************************************
		DNG_Nummer = Dongle_Count(DNG_Port)
		
		DNG_Version = Dongle_Version(DNG_Nummer, DNG_Port)
		
		If DNG_Version <= 0 Then
			MsgBox("Error while reading Dongle version!")
		Else
			VerMinor = CShort(DNG_Version And 65535)
			VerMajor = CShort(DNG_Version \ Shift16)
			MsgBox("This Dongle has the version number: " & VerMajor & "." & VerMinor)
		End If
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub Read_Dongle_SerNr()
		
		RetCode = Init_MatrixAPI()
		
		'***********************************************************
		'* Read the SerialNo. of last Dongle at DNG_Port via       *
		'* UserCode and display.                                   *
		'***********************************************************
		DNG_Nummer = Dongle_Count(DNG_Port)
		
		DNG_SerNr = Dongle_ReadSerNr(UserCode, DNG_Nummer, DNG_Port)
		
		If DNG_SerNr < 0 Then
			MsgBox("Error " & DNG_SerNr & " while reading the Dongle SerialNo.!")
			RetCode = Release_MatrixAPI()
			Exit Sub
		End If
		
		MsgBox("SerialNo. of the Dongle:  (dec) " & DNG_SerNr & "    (hex) " & Hex(DNG_SerNr))
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub Read_Dongle_Data()
		Dim i As Object
		
		Dim MessageString As String
		
		RetCode = Init_MatrixAPI()
		
		'***********************************************************
		'* Read 3 data fields from last Dongle at DNG_Port via     *
		'* UserCode and display.                                   *
		'***********************************************************
		DNG_Nummer = Dongle_Count(DNG_Port)
		
		RetCode = Dongle_ReadData(UserCode, DataIn(0), 3, DNG_Nummer, DNG_Port)
		
		If RetCode < 0 Then
			MsgBox("Error " & RetCode & " while reading the Dongle data!")
			RetCode = Release_MatrixAPI()
			Exit Sub
		End If
		
		MessageString = "Content of dongle variables: " & Chr(13) & Chr(13)
		For i = 0 To 2
			'UPGRADE_WARNING: Die Standardeigenschaft des Objekts i konnte nicht aufgelöst werden. Klicken Sie hier fE weitere Informationen: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"'
			MessageString = MessageString & "Var" & VB6.Format(i + 1, "0000") & ":  " & DataIn(i) & Chr(13)
		Next i
		MsgBox(MessageString)
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub Write_Dongle_Data()
		Dim i As Object
		
		RetCode = Init_MatrixAPI()
		
		'***********************************************************
		'* Save 3 data fields in last Dongle at DNG_Port via       *
		'* UserCode. The values 101,102,103 are saved as example   *
		'***********************************************************
		DNG_Nummer = Dongle_Count(DNG_Port)
		
		For i = 0 To 2
			'UPGRADE_WARNING: Die Standardeigenschaft des Objekts i konnte nicht aufgelöst werden. Klicken Sie hier fE weitere Informationen: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"'
			DataOut(i) = 101 + i
		Next i
		
		RetCode = Dongle_WriteData(UserCode, DataOut(0), 3, DNG_Nummer, DNG_Port)
		
		If RetCode < 0 Then
			MsgBox("Error " & RetCode & " while writing Dongle data!")
		Else
			MsgBox("The Dongle data have been written successfully!")
		End If
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub Dongle_Encrypt_Decrypt()
		
		Dim MessageString As String
		
		RetCode = Init_MatrixAPI()
		
		DataBlock(0) = 1234567890 '* Clear Data *
		DataBlock(1) = 1234567890 '* Clear Data *
		
		MessageString = "Clear Data:   " & vbTab & "(dec)  " & DataBlock(0) & vbTab & "   " & DataBlock(1) & Chr(13) & vbTab & vbTab & "(hex)  " & Hex(DataBlock(0)) & vbTab & "   " & Hex(DataBlock(1)) & Chr(13) & Chr(13)
		
		DNG_Nummer = Dongle_Count(DNG_Port)
		
		'***********************************************************
		'* Encrypt DataBlock over the Dongle                       *
		'***********************************************************
		RetCode = Dongle_EncryptData(UserCode, DataBlock(0), DNG_Nummer, DNG_Port)
		
		If RetCode < 0 Then
			MsgBox("Error " & RetCode)
			RetCode = Release_MatrixAPI()
			Exit Sub
		End If
		
		MessageString = MessageString & "Encrypted Data: " & vbTab & "(dec)  " & DataBlock(0) & vbTab & "   " & DataBlock(1) & Chr(13) & vbTab & vbTab & "(hex)  " & Hex(DataBlock(0)) & vbTab & "   " & Hex(DataBlock(1)) & Chr(13) & Chr(13)
		
		'***********************************************************
		'* Decrypt DataBlock over the Dongle                       *
		'***********************************************************
		RetCode = Dongle_DecryptData(UserCode, DataBlock(0), DNG_Nummer, DNG_Port)
		
		If RetCode < 0 Then
			MsgBox("Error " & RetCode)
			RetCode = Release_MatrixAPI()
			Exit Sub
		End If
		
		MsgBox(MessageString & "Decrypted Data: " & vbTab & "(dec)  " & DataBlock(0) & vbTab & "   " & DataBlock(1) & Chr(13) & vbTab & vbTab & "(hex)  " & Hex(DataBlock(0)) & vbTab & "   " & Hex(DataBlock(1)) & Chr(13) & Chr(13))
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub Version_of_API()
		
		Dim RetVer As Integer
		
		RetCode = Init_MatrixAPI()
		
		'***********************************************************
		'* Read the version of the Matrix-API                      *
		'***********************************************************
		RetVer = GetVersionAPI()
		
		If RetVer = 0 Then
			MsgBox("Error while reading API version!")
		Else
			VerMinor = CShort(RetVer And 65535)
			VerMajor = CShort(RetVer \ Shift16)
			MsgBox("The Matrix-API have the version: " & VerMajor & "." & VerMinor)
		End If
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub Dongle_Network()
		
		'*************************************************************
		'* Network                                                   *
		'* Example: the dongle is prepared with the value 7 in       *
		'*          Var0002, meaning AppSlot=2 and max. Users = 7    *
		'*          (AppSlot2 = max. 7 Users)                        *
		'*************************************************************
		
		RetCode = Init_MatrixAPI()
		
		'*************************************************************
		'* Max. Users stored in AppSlot 2 (Var0002 of the dongle)    *
		'*************************************************************
		AppSlot = 2
		
		'*************************************************************
		'* Activate Network access into the Matrix-API               *
		'*************************************************************
		Dim MxNetFile As String
		MxNetFile = "D:\TEMP\MXNET2.DAT"
		RetCode = SetConfig_MatrixNet(1, MxNetFile)
		
		'*************************************************************
		'* Read the Dongle count from MxNetFile                      *
		'* !! DNG_Port will be ignored in the Network mode !!        *
		'*************************************************************
		DNG_Nummer = Dongle_Count(DNG_Port)
		
		'*************************************************************
		'* LogIn will lock one User-Slot and the returned value is   *
		'* the remaining free User-Slots (for our Example RetCode=6) *
		'*************************************************************
		RetCode = LogIn_MatrixNet(UserCode, AppSlot, DNG_Nummer)
		If RetCode < 0 Then
			If RetCode = -31 Then
				MsgBox("All Users are active!" & Chr(13) & "No more User-Slots free")
			Else
				MsgBox("LogIn failed !  " & RetCode)
			End If
		Else
			MsgBox("LogIn successfull !  Free User-Slots: " & RetCode & Chr(13) & Chr(13) & "Now you can check the Active Users List in MxNet-Server." & Chr(13) & "After clicking the OK button, the User will be Logged Out and" & Chr(13) & "removed from the Active Users List in MxNet-Server.")
		End If
		
		'*************************************************************
		'* LogOut will free the User-Slot and the returned value is  *
		'* the remaining free User-Slots (for our Example RetCode=7) *
		'*************************************************************
		RetCode = LogOut_MatrixNet(UserCode, AppSlot, DNG_Nummer)
		If RetCode < 0 Then
			MsgBox("LogOut failed !  " & RetCode)
		Else
			MsgBox("LogOut successfull ! Free User-Slots: " & RetCode)
		End If
		
		'*************************************************************
		'* Deactivate Network access into the Matrix-API             *
		'*************************************************************
		RetCode = SetConfig_MatrixNet(0, vbNullString)
		
		RetCode = Release_MatrixAPI()
		
	End Sub
	
	Public Sub App_Encrypt()
		
		Dim MessageString As String
		Dim Key(4) As Integer
		Dim Data(2) As Integer
		
		Data(0) = &H499602D2
		Data(1) = &H499602D2
		
		Key(0) = &H423A612E
		Key(1) = &H423A8C95
		Key(2) = &H8474C25C
		Key(3) = &H8474EDC3
		
		MessageString = "Clear Data:   " & vbTab & "(dec)  " & Data(0) & vbTab & "      " & Data(1) & Chr(13) & vbTab & vbTab & "(hex)  " & Hex(Data(0)) & vbTab & "      " & Hex(Data(1)) & Chr(13) & Chr(13) & "Key:          " & vbTab & "(dec)  " & Key(0) & vbTab & "      " & Key(1) & vbTab & "  " & Key(2) & vbTab & Key(3) & Chr(13) & vbTab & vbTab & "(hex)  " & Hex(Key(0)) & vbTab & "      " & Hex(Key(1)) & vbTab & "  " & Hex(Key(2)) & vbTab & Hex(Key(3)) & Chr(13) & Chr(13)
		
		Call MxApp_Encrypt(Data, Key)
		
		'*** Display Data(0)/Data(1) as Long ***
		MsgBox(MessageString & "Encrypted Data: " & vbTab & "(dec)  " & Data(0) & vbTab & "      " & Data(1) & Chr(13) & vbTab & vbTab & "(hex)  " & Hex(Data(0)) & vbTab & "      " & Hex(Data(1)) & Chr(13) & Chr(13))
		
	End Sub
End Module