' !! this file should not be modified

Imports System.Runtime.InteropServices
Imports System.Text

Module DinkeyChange_code

    ' declarations for functions exported by DinkeyChange.dll
    Declare Ansi Function DCGetInfo32 Lib "DinkeyChange.dll" Alias "DCGetInfo" (ByVal type_mask As Integer, ByVal model_mask As Integer, ByVal prodcode_mask As String, ByVal array_length As Integer, _
        ByRef num_found As Integer, ByVal type_array() As Integer, ByVal model_array() As Integer, ByVal prodcode_array() As Byte, ByVal dongle_number() As UInt32, ByVal update_number() As Integer) As Integer
    Declare Ansi Function DCGetDiagnosticInfo32 Lib "DinkeyChange.dll" Alias "DCGetDiagnosticInfo" (ByVal filename As String) As Integer
    Declare Ansi Function DCDoUpdateCodeString32 Lib "DinkeyChange.dll" Alias "DCDoUpdateCodeString" (ByVal update_code_string As String, ByRef confirmation_code As Integer, ByRef extended_error As Integer) As Integer
    Declare Ansi Function DCDoUpdateCodeFromFile32 Lib "DinkeyChange.dll" Alias "DCDoUpdateCodeFromFile" (ByVal filename As String, ByRef confirmation_code As Integer, ByRef extended_error As Integer) As Integer
    Declare Ansi Function DCRestoreDinkeyFDLite32 Lib "DinkeyChange.dll" Alias "DCRestoreDinkeyFDLite" () As Integer
    Declare Ansi Function DCGetMachineID32 Lib "DinkeyChange.dll" Alias "DCGetMachineID" (ByRef machineID As UInt32, ByRef extended_error As Integer) As Integer
    Declare Ansi Function DCDownloadTempSoftwareKey32 Lib "DinkeyChange.dll" Alias "DCDownloadTempSoftwareKey" (ByVal machineID As UInt32, ByRef extended_error As Integer) As Integer
    Declare Ansi Function DCDownloadDemoSoftwareKey32 Lib "DinkeyChange.dll" Alias "DCDownloadDemoSoftwareKey" (ByVal machineID As UInt32, ByVal prodcode As String, ByVal model As Integer, ByRef extended_error As Integer) As Integer
	
    ' declarations for functions exported by DinkeyChange64.dll
    Declare Ansi Function DCGetInfo64 Lib "DinkeyChange64.dll" Alias "DCGetInfo" (ByVal type_mask As Integer, ByVal model_mask As Integer, ByVal prodcode_mask As String, ByVal array_length As Integer, _
        ByRef num_found As Integer, ByVal type_array() As Integer, ByVal model_array() As Integer, ByVal prodcode_array() As Byte, ByVal dongle_number() As UInt32, ByVal update_number() As Integer) As Integer
    Declare Ansi Function DCGetDiagnosticInfo64 Lib "DinkeyChange64.dll" Alias "DCGetDiagnosticInfo" (ByVal filename As String) As Integer
    Declare Ansi Function DCDoUpdateCodeString64 Lib "DinkeyChange64.dll" Alias "DCDoUpdateCodeString" (ByVal update_code_string As String, ByRef confirmation_code As Integer, ByRef extended_error As Integer) As Integer
    Declare Ansi Function DCDoUpdateCodeFromFile64 Lib "DinkeyChange64.dll" Alias "DCDoUpdateCodeFromFile" (ByVal filename As String, ByRef confirmation_code As Integer, ByRef extended_error As Integer) As Integer
    Declare Ansi Function DCRestoreDinkeyFDLite64 Lib "DinkeyChange64.dll" Alias "DCRestoreDinkeyFDLite" () As Integer
    Declare Ansi Function DCGetMachineID64 Lib "DinkeyChange64.dll" Alias "DCGetMachineID" (ByRef machineID As UInt32, ByRef extended_error As Integer) As Integer
    Declare Ansi Function DCDownloadTempSoftwareKey64 Lib "DinkeyChange64.dll" Alias "DCDownloadTempSoftwareKey" (ByVal machineID As UInt32, ByRef extended_error As Integer) As Integer
    Declare Ansi Function DCDownloadDemoSoftwareKey64 Lib "DinkeyChange64.dll" Alias "DCDownloadDemoSoftwareKey" (ByVal machineID As UInt32, ByVal prodcode As String, ByVal model As Integer, ByRef extended_error As Integer) As Integer

    'routines that ensure the relevant dll is called depending on whether we are running on 32-bit or 64-bit Windows
    Function DCGetInfo(ByVal type_mask As Integer, ByVal model_mask As Integer, ByVal prodcode_mask As String, ByVal array_length As Integer, _
        ByRef num_found As Integer, ByVal type_array() As Integer, ByVal model_array() As Integer, ByVal prodcode_array() As Byte, ByVal dongle_number() As UInt32, ByVal update_number() As Integer) As Integer
        If (IntPtr.Size = 4) Then
            DCGetInfo = DCGetInfo32(type_mask, model_mask, prodcode_mask, array_length, num_found, type_array, model_array, prodcode_array, dongle_number, update_number)
        Else
            DCGetInfo = DCGetInfo64(type_mask, model_mask, prodcode_mask, array_length, num_found, type_array, model_array, prodcode_array, dongle_number, update_number)
        End If
    End Function

    Function DCGetDiagnosticInfo(ByVal filename As String) As Integer
        If (IntPtr.Size = 4) Then
            DCGetDiagnosticInfo = DCGetDiagnosticInfo32(filename)
        Else
            DCGetDiagnosticInfo = DCGetDiagnosticInfo64(filename)
        End If
    End Function

    Function DCDoUpdateCodeString(ByVal update_code_string As String, ByRef confirmation_code As Integer, ByRef extended_error As Integer) As Integer
        If (IntPtr.Size = 4) Then
            DCDoUpdateCodeString = DCDoUpdateCodeString32(update_code_string, confirmation_code, extended_error)
        Else
            DCDoUpdateCodeString = DCDoUpdateCodeString64(update_code_string, confirmation_code, extended_error)
        End If
    End Function

    Function DCDoUpdateCodeFromFile(ByVal filename As String, ByRef confirmation_code As Integer, ByRef extended_error As Integer) As Integer
        If (IntPtr.Size = 4) Then
            DCDoUpdateCodeFromFile = DCDoUpdateCodeFromFile32(filename, confirmation_code, extended_error)
        Else
            DCDoUpdateCodeFromFile = DCDoUpdateCodeFromFile64(filename, confirmation_code, extended_error)
        End If
    End Function

    Function DCRestoreDinkeyFDLite() As Integer
        If (IntPtr.Size = 4) Then
            DCRestoreDinkeyFDLite = DCRestoreDinkeyFDLite32()
        Else
            DCRestoreDinkeyFDLite = DCRestoreDinkeyFDLite64()
        End If
    End Function
	
    Function DCGetMachineID(ByRef machineID As UInt32, ByRef extended_error As Integer) As Integer
        If (IntPtr.Size = 4) Then
            DCGetMachineID = DCGetMachineID32(machineID, extended_error)
        Else
            DCGetMachineID = DCGetMachineID64(machineID, extended_error)
        End If
    End Function

    Function DCDownloadTempSoftwareKey(ByVal machineID As UInt32, ByRef extended_error As Integer) As Integer
        If (IntPtr.Size = 4) Then
            DCDownloadTempSoftwareKey = DCDownloadTempSoftwareKey32(machineID, extended_error)
        Else
            DCDownloadTempSoftwareKey = DCDownloadTempSoftwareKey64(machineID, extended_error)
        End If
    End Function

    Function DCDownloadDemoSoftwareKey(ByVal machineID As UInt32, ByVal prodcode As String, ByVal model As Integer, ByRef extended_error As Integer) As Integer
        If (IntPtr.Size = 4) Then
            DCDownloadDemoSoftwareKey = DCDownloadDemoSoftwareKey32(machineID, prodcode, model, extended_error)
        Else
            DCDownloadDemoSoftwareKey = DCDownloadDemoSoftwareKey64(machineID, prodcode, model, extended_error)
        End If
    End Function
	
    ' routine to extract product code string from array of product codes
    Function GetProductCode(ByRef prodcode() As Byte, ByVal index As Integer) As String
        Dim i As Integer
        Dim ascii As New ASCIIEncoding()

        For i = 0 To 8
            If prodcode((index * 9) + i) = 0 Then
                Exit For
            End If
        Next
        Return ascii.GetString(prodcode, index * 9, i)
    End Function

    ' maximum number of USB devices that can be attached to a machine at any time
    Public Const MAX_USB_DEVICES As Integer = 128
    Public Const MAX_PRODCODE_LEN As Integer = 9

    ' mask values
    Public Const TYPE_MASK_PRO As Integer = 1
    Public Const TYPE_MASK_FD As Integer = 2
    Public Const TYPE_MASK_ALL As Integer = TYPE_MASK_PRO Or TYPE_MASK_FD

    Public Const MODEL_MASK_LITE As Integer = 1
    Public Const MODEL_MASK_PLUS As Integer = 2
    Public Const MODEL_MASK_NET As Integer = 4
    Public Const MODEL_MASK_ALL As Integer = MODEL_MASK_LITE Or MODEL_MASK_PLUS Or MODEL_MASK_NET
    Public Const MODEL_MASK_DEFAULT As Integer = MODEL_MASK_PLUS Or MODEL_MASK_NET

    ' type values for DCGetInfo (returned in the array)
    Public Const TYPE_PRO As Integer = 1
    Public Const TYPE_FD As Integer = 2

    ' model values for DCGetInfo (returned in the array)
    Public Const MODEL_LITE As Integer = 1
    Public Const MODEL_PLUS As Integer = 2
    Public Const MODEL_NET5 As Integer = 4
    Public Const MODEL_NETU As Integer = 7

    ' model values for DCDownloadDemoSoftwareKey
    Public Const SWKEY_MODEL_DEFAULT As Integer = &HFFFFFFFF             ' NB this assumes that the Demo Software Key Template was created with only one valid dongle model
    Public Const SWKEY_MODEL_PRO_LITE As Integer = 0
    Public Const SWKEY_MODEL_PRO_PLUS As Integer = 1
    Public Const SWKEY_MODEL_PRO_NET As Integer = 2
    Public Const SWKEY_MODEL_FD_LITE As Integer = 3
    Public Const SWKEY_MODEL_FD_PLUS As Integer = 4
    Public Const SWKEY_MODEL_FD_NET As Integer = 5
    
End Module
    