Option Strict Off
Option Explicit On
Module Matrix
	'******************************************************************
	'* MATRIX.VB
	'* Example for VisualBasic.NET
	'*
	'* Copyright (C) TDi GmbH
	'******************************************************************
	
	
    Dim RetCode As Short
    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
    Const DNG_Port As Short = 85
	
	
    Declare Function rInit_MatrixAPI Lib "mxnet1_api.dll" () As Short

    Declare Function rRelease_MatrixAPI Lib "mxnet1_api.dll" () As Short

    Declare Function rGetVersionAPI Lib "mxnet1_api.dll" () As Integer

    Declare Function rGetVersionDRV_USB Lib "mxnet1_api.dll" () As Integer

    Declare Function rDongle_Count Lib "mxnet1_api.dll" (ByVal DNG_Port As Short) As Short

    Declare Function rDongle_Version Lib "mxnet1_api.dll" (ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Integer

    Declare Function rDongle_Model Lib "mxnet1_api.dll" (ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short

    Declare Function rDongle_MemSize Lib "mxnet1_api.dll" (ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short

    Declare Function rDongle_ReadData Lib "mxnet1_api.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 rDongle_ReadDataEx Lib "mxnet1_api.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 rDongle_WriteData Lib "mxnet1_api.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 rDongle_WriteDataEx Lib "mxnet1_api.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 rDongle_WriteKey Lib "mxnet1_api.dll" (ByVal UserCode As Integer, ByRef KeyData As Integer, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short

    Declare Function rDongle_ReadSerNr Lib "mxnet1_api.dll" (ByVal UserCode As Integer, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Integer

    Declare Function rDongle_EncryptData Lib "mxnet1_api.dll" (ByVal UserCode As Integer, ByRef DataBlock As Integer, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short

    Declare Function rDongle_DecryptData Lib "mxnet1_api.dll" (ByVal UserCode As Integer, ByRef DataBlock As Integer, ByVal DNG_Nummer As Short, ByVal DNG_Port As Short) As Short

    Declare Function rSetConfig_MatrixNet Lib "mxnet1_api.dll" (ByVal nAccess As Short, ByVal nFile As String) As Short

    Declare Function rLogIn_MatrixNet Lib "mxnet1_api.dll" (ByVal UserCode As Integer, ByVal AppSlot As Short, ByVal DNG_Nr As Short) As Short

    Declare Function rLogOut_MatrixNet Lib "mxnet1_api.dll" (ByVal UserCode As Integer, ByVal AppSlot As Short, ByVal DNG_Nr As Short) As Short
	

	
    Public Sub SetActiveDongle(ByVal DngNr As Integer)
        DNG_Nummer = DngNr
    End Sub
	
	
    Public Sub Number_of_Dongles()

        RetCode = rInit_MatrixAPI()
        If RetCode < 0 Then
            MsgBox("初期化エラー")
            Exit Sub
        End If

        DNG_Count = rDongle_Count(DNG_Port)

        If DNG_Count <= 0 Then
            MsgBox("ドングルが見つかりません")
        Else
            MsgBox("ドングル数: " & DNG_Count)
        End If

        RetCode = rRelease_MatrixAPI()

    End Sub
	
    Public Sub Memory_of_Dongle()

        RetCode = rInit_MatrixAPI()
        If RetCode < 0 Then
            MsgBox("初期化エラー")
            Exit Sub
        End If

        DNG_Mem = rDongle_MemSize(DNG_Nummer, DNG_Port)

        If DNG_Mem <= 0 Then
            MsgBox("メモリサイズ確認中にエラー")
            Exit Sub
        End If

        DNG_MaxVar = DNG_Mem / 4

        MsgBox("メモリサイズ: " & DNG_MaxVar)

        RetCode = rRelease_MatrixAPI()

    End Sub

    Public Sub Version_of_Dongle()

        RetCode = rInit_MatrixAPI()
        If RetCode < 0 Then
            MsgBox("初期化エラー")
            Exit Sub
        End If


        DNG_Version = rDongle_Version(DNG_Nummer, DNG_Port)

        If DNG_Version <= 0 Then
            MsgBox("バージョン取得中にエラー")
        Else
            VerMinor = CShort(DNG_Version And 65535)
            VerMajor = CShort(DNG_Version \ Shift16)
            MsgBox("バージョン: " & VerMajor & "." & VerMinor)
        End If

        RetCode = rRelease_MatrixAPI()

    End Sub

    Public Sub Read_Dongle_SerNr()

        RetCode = rInit_MatrixAPI()
        If RetCode < 0 Then
            MsgBox("初期化エラー")
            Exit Sub
        End If


        DNG_SerNr = rDongle_ReadSerNr(UserCode, DNG_Nummer, DNG_Port)

        If DNG_SerNr < 0 Then
            MsgBox("エラー: " & DNG_SerNr)
            RetCode = rRelease_MatrixAPI()
            Exit Sub
        End If

        MsgBox("シリアル番号:  (dec) " & DNG_SerNr & "    (hex) " & Hex(DNG_SerNr))

        RetCode = rRelease_MatrixAPI()

    End Sub

    Public Sub Read_Dongle_Data()
        Dim i As Object

        Dim MessageString As String

        RetCode = rInit_MatrixAPI()
        If RetCode < 0 Then
            MsgBox("初期化エラー")
            Exit Sub
        End If

        '***********************************************************
        '* フィールド1から3の読み込み                              *
        '*                                                         *
        '***********************************************************

        RetCode = rDongle_ReadData(UserCode, DataIn(0), 3, DNG_Nummer, DNG_Port)

        If RetCode < 0 Then
            MsgBox("エラー: " & RetCode)
            RetCode = rRelease_MatrixAPI()
            Exit Sub
        End If

        MessageString = "フィールドデータ: " & Chr(13) & Chr(13)
        For i = 0 To 2
            MessageString = MessageString & "Var" & VB6.Format(i + 1, "0000") & ":  " & DataIn(i) & Chr(13)
        Next i
        MsgBox(MessageString)

        RetCode = rRelease_MatrixAPI()

    End Sub

    Public Sub Write_Dongle_Data()
        Dim i As Object

        RetCode = rInit_MatrixAPI()
        If RetCode < 0 Then
            MsgBox("初期化エラー")
            Exit Sub
        End If

        '***********************************************************
        '* フィールド1から3への書き込み                            *
        '* 値 101,102,103                                          *
        '***********************************************************

        For i = 0 To 2
            DataOut(i) = 101 + i
        Next i

        RetCode = rDongle_WriteData(UserCode, DataOut(0), 3, DNG_Nummer, DNG_Port)

        If RetCode < 0 Then
            MsgBox("エラー: " & RetCode)
        Else
            MsgBox("フィールド1-3に 101,102,103を書き込みました")
        End If

        RetCode = rRelease_MatrixAPI()

    End Sub
    Public Sub Write_Encryption_key(ByVal enckeys() As Integer)
        Dim i As Integer

        RetCode = rInit_MatrixAPI()
        If RetCode < 0 Then
            MsgBox("初期化エラー")
            Exit Sub
        End If

        For i = 0 To 3
            KeyData(i) = enckeys(i)
        Next i

        RetCode = rDongle_WriteKey(UserCode, KeyData(0), DNG_Nummer, DNG_Port)

        If RetCode < 0 Then
            MsgBox("エラー: " & RetCode)
        Else
            MsgBox("暗号鍵を書き込みました")
        End If

        RetCode = rRelease_MatrixAPI()

    End Sub

    Public Sub Dongle_Encrypt_Decrypt()

        Dim MessageString As String

        RetCode = rInit_MatrixAPI()
        If RetCode < 0 Then
            MsgBox("初期化エラー")
            Exit Sub
        End If

        DataBlock(0) = 1234567890 '* Clear Data *
        DataBlock(1) = 1234567890 '* Clear Data *

        MessageString = "プレーンデータ:   " & vbTab & "(dec)  " & DataBlock(0) & vbTab & "   " & DataBlock(1) & Chr(13) & vbTab & vbTab & "(hex)  " & Hex(DataBlock(0)) & vbTab & "   " & Hex(DataBlock(1)) & Chr(13) & Chr(13)

        '***********************************************************
        '* Encrypt DataBlock over the Dongle                       *
        '***********************************************************
        RetCode = rDongle_EncryptData(UserCode, DataBlock(0), DNG_Nummer, DNG_Port)

        If RetCode < 0 Then
            MsgBox("エラー: " & RetCode)
            RetCode = rRelease_MatrixAPI()
            Exit Sub
        End If

        MessageString = MessageString & "暗号化後データ: " & 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 = rDongle_DecryptData(UserCode, DataBlock(0), DNG_Nummer, DNG_Port)

        If RetCode < 0 Then
            MsgBox("Error " & RetCode)
            RetCode = rRelease_MatrixAPI()
            Exit Sub
        End If

        MsgBox(MessageString & "復号化後データ: " & vbTab & "(dec)  " & DataBlock(0) & vbTab & "   " & DataBlock(1) & Chr(13) & vbTab & vbTab & "(hex)  " & Hex(DataBlock(0)) & vbTab & "   " & Hex(DataBlock(1)) & Chr(13) & Chr(13))

        RetCode = rRelease_MatrixAPI()

    End Sub

    Public Sub Version_of_API()

        Dim RetVer As Integer

        RetCode = rInit_MatrixAPI()
        If RetCode < 0 Then
            MsgBox("初期化エラー")
            Exit Sub
        End If

        RetVer = rGetVersionAPI()

        If RetVer = 0 Then
            MsgBox("エラー: " & RetVer)
        Else
            VerMinor = CShort(RetVer And 65535)
            VerMajor = CShort(RetVer \ Shift16)
            MsgBox("Matrix-APIバージョン: " & VerMajor & "." & VerMinor)
        End If

        RetCode = rRelease_MatrixAPI()

    End Sub

    Public Sub Dongle_Network()

        RetCode = rInit_MatrixAPI()
        If RetCode < 0 Then
            MsgBox("初期化エラー")
            Exit Sub
        End If

        '*************************************************************
        '* AppSlot 2がライセンス数を保持　　　　　　　　　　　　　   *
        '*************************************************************
        AppSlot = 2


        '*************************************************************
        '* LogInはAppSlotのライセンス数を1減少させ、その値を返す　　*
        '* 　　　　　　　　　　　　　　　　　　　　　　　　　　　　  *
        '*************************************************************
        RetCode = rLogIn_MatrixNet(UserCode, AppSlot, DNG_Nummer)
        If RetCode < 0 Then
            If RetCode = -31 Then
                MsgBox("ライセンス上限!" & Chr(13) & "appSlotに空きはありません")
            Else
                MsgBox("ログインエラー " & RetCode)
            End If
        Else
            MsgBox("ログイン成功!  空きスロット数: " & RetCode & Chr(13) & Chr(13))
        End If

        '*************************************************************
        '* LogOutはAppSlotのライセンス数を1増加させ、その値を返す   *
        '*                                                           *
        '*************************************************************
        RetCode = rLogOut_MatrixNet(UserCode, AppSlot, DNG_Nummer)
        If RetCode < 0 Then
            MsgBox("ログアウト失敗!  " & RetCode)
        Else
            MsgBox("ログアウト成功 ! 空きスロット数: " & RetCode)
        End If


        RetCode = rRelease_MatrixAPI()

    End Sub
	
End Module
