' !! We strongly recommend that you read the "readme.txt" file in the sample code folder.
' !! It contains important instructions on how to use the sample code

Imports System.Text

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GetDongleInfo As System.Windows.Forms.Button
    Friend WithEvents label1 As System.Windows.Forms.Label
    Friend WithEvents DiagFilename As System.Windows.Forms.TextBox
    Friend WithEvents WriteDiags As System.Windows.Forms.Button
    Friend WithEvents DoUpdateCodeString As System.Windows.Forms.Button
    Friend WithEvents UpdateCodeString As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents DoUpdateCodeFile As System.Windows.Forms.Button
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents UpdateCodeFile As System.Windows.Forms.TextBox
    Friend WithEvents RestoreFDLite As System.Windows.Forms.Button
    Friend WithEvents GetMachineID As System.Windows.Forms.Button
    Friend WithEvents DownloadTempSoftwareKey As System.Windows.Forms.Button
    Friend WithEvents ShowMachineID As System.Windows.Forms.TextBox
    Friend WithEvents label4 As System.Windows.Forms.Label
    Friend WithEvents ProdCode As System.Windows.Forms.TextBox
    Friend WithEvents DownloadDemoSoftwareKey As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GetDongleInfo = New System.Windows.Forms.Button
        Me.label1 = New System.Windows.Forms.Label
        Me.DiagFilename = New System.Windows.Forms.TextBox
        Me.WriteDiags = New System.Windows.Forms.Button
        Me.DoUpdateCodeString = New System.Windows.Forms.Button
        Me.UpdateCodeString = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.DoUpdateCodeFile = New System.Windows.Forms.Button
        Me.UpdateCodeFile = New System.Windows.Forms.TextBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.RestoreFDLite = New System.Windows.Forms.Button
        Me.GetMachineID = New System.Windows.Forms.Button
        Me.DownloadTempSoftwareKey = New System.Windows.Forms.Button
        Me.ShowMachineID = New System.Windows.Forms.TextBox
        Me.label4 = New System.Windows.Forms.Label
        Me.ProdCode = New System.Windows.Forms.TextBox
        Me.DownloadDemoSoftwareKey = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'GetDongleInfo
        '
        Me.GetDongleInfo.Location = New System.Drawing.Point(48, 24)
        Me.GetDongleInfo.Name = "GetDongleInfo"
        Me.GetDongleInfo.Size = New System.Drawing.Size(216, 24)
        Me.GetDongleInfo.TabIndex = 0
        Me.GetDongleInfo.Text = "Get Dongle Information"
        '
        'label1
        '
        Me.label1.Location = New System.Drawing.Point(16, 72)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(296, 16)
        Me.label1.TabIndex = 1
        Me.label1.Text = "Enter diagnostic filename:"
        '
        'DiagFilename
        '
        Me.DiagFilename.Location = New System.Drawing.Point(16, 88)
        Me.DiagFilename.Name = "DiagFilename"
        Me.DiagFilename.Size = New System.Drawing.Size(288, 20)
        Me.DiagFilename.TabIndex = 2
        Me.DiagFilename.Text = ""
        '
        'WriteDiags
        '
        Me.WriteDiags.Location = New System.Drawing.Point(56, 120)
        Me.WriteDiags.Name = "WriteDiags"
        Me.WriteDiags.Size = New System.Drawing.Size(192, 23)
        Me.WriteDiags.TabIndex = 3
        Me.WriteDiags.Text = "Write Diagnostics to File"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 171)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(296, 16)
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "Enter short Update Code here:"
        '
        'UpdateCodeString
        '
        Me.UpdateCodeString.Location = New System.Drawing.Point(16, 187)
        Me.UpdateCodeString.Name = "UpdateCodeString"
        Me.UpdateCodeString.Size = New System.Drawing.Size(288, 20)
        Me.UpdateCodeString.TabIndex = 5
        Me.UpdateCodeString.Text = ""
        '
        'DoUpdateCodeString
        '
        Me.DoUpdateCodeString.Location = New System.Drawing.Point(56, 219)
        Me.DoUpdateCodeString.Name = "DoUpdateCodeString"
        Me.DoUpdateCodeString.Size = New System.Drawing.Size(192, 23)
        Me.DoUpdateCodeString.TabIndex = 6
        Me.DoUpdateCodeString.Text = "Make Changes"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 272)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(296, 16)
        Me.Label3.TabIndex = 7
        Me.Label3.Text = "Enter path to Update Code file:"
        '
        'UpdateCodeFile
        '
        Me.UpdateCodeFile.Location = New System.Drawing.Point(16, 288)
        Me.UpdateCodeFile.Name = "UpdateCodeFile"
        Me.UpdateCodeFile.Size = New System.Drawing.Size(288, 20)
        Me.UpdateCodeFile.TabIndex = 8
        Me.UpdateCodeFile.Text = ""
        '
        'DoUpdateCodeFile
        '
        Me.DoUpdateCodeFile.Location = New System.Drawing.Point(56, 320)
        Me.DoUpdateCodeFile.Name = "DoUpdateCodeFile"
        Me.DoUpdateCodeFile.Size = New System.Drawing.Size(192, 23)
        Me.DoUpdateCodeFile.TabIndex = 9
        Me.DoUpdateCodeFile.Text = "Make Changes"
        '
        'RestoreFDLite
        '
        Me.RestoreFDLite.Location = New System.Drawing.Point(56, 376)
        Me.RestoreFDLite.Name = "RestoreFDLite"
        Me.RestoreFDLite.Size = New System.Drawing.Size(192, 23)
        Me.RestoreFDLite.TabIndex = 10
        Me.RestoreFDLite.Text = "Restore FD Lite"
        '
        'GetMachineID
        '
        Me.GetMachineID.Location = New System.Drawing.Point(24, 432)
        Me.GetMachineID.Name = "GetMachineID"
        Me.GetMachineID.Size = New System.Drawing.Size(128, 23)
        Me.GetMachineID.TabIndex = 11
        Me.GetMachineID.Text = "Get Machine ID"
        '
        'ShowMachineID
        '
        Me.ShowMachineID.Location = New System.Drawing.Point(168, 432)
        Me.ShowMachineID.Name = "ShowMachineID"
        Me.ShowMachineID.Size = New System.Drawing.Size(136, 20)
        Me.ShowMachineID.TabIndex = 12
        Me.ShowMachineID.Text = ""
        '
        'DownloadTempSoftwareKey
        '
        Me.DownloadTempSoftwareKey.Location = New System.Drawing.Point(56, 488)
        Me.DownloadTempSoftwareKey.Name = "DownloadTempSoftwareKey"
        Me.DownloadTempSoftwareKey.Size = New System.Drawing.Size(192, 23)
        Me.DownloadTempSoftwareKey.TabIndex = 13
        Me.DownloadTempSoftwareKey.Text = "Download Temporary Software Key"
        '
        'label4
        '
        Me.label4.Location = New System.Drawing.Point(16, 541)
        Me.label4.Name = "label4"
        Me.label4.Size = New System.Drawing.Size(296, 16)
        Me.label4.TabIndex = 14
        Me.label4.Text = "Demo Software Key Product Code:"
        '
        'ProdCode
        '
        Me.ProdCode.Location = New System.Drawing.Point(16, 557)
        Me.ProdCode.Name = "ProdCode"
        Me.ProdCode.Size = New System.Drawing.Size(288, 20)
        Me.ProdCode.TabIndex = 15
        Me.ProdCode.Text = ""
        '
        'DownloadTempSoftwareKey
        '
        Me.DownloadDemoSoftwareKey.Location = New System.Drawing.Point(56, 590)
        Me.DownloadDemoSoftwareKey.Name = "DownloadDemoSoftwareKey"
        Me.DownloadDemoSoftwareKey.Size = New System.Drawing.Size(192, 23)
        Me.DownloadDemoSoftwareKey.TabIndex = 16
        Me.DownloadDemoSoftwareKey.Text = "Download Demo Software Key"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(320, 636)
        Me.Controls.Add(Me.DownloadDemoSoftwareKey)
        Me.Controls.Add(Me.ProdCode)
        Me.Controls.Add(Me.label4)
        Me.Controls.Add(Me.ShowMachineID)
        Me.Controls.Add(Me.DownloadTempSoftwareKey)
        Me.Controls.Add(Me.GetMachineID)
        Me.Controls.Add(Me.RestoreFDLite)
        Me.Controls.Add(Me.DoUpdateCodeFile)
        Me.Controls.Add(Me.UpdateCodeFile)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.DoUpdateCodeString)
        Me.Controls.Add(Me.UpdateCodeString)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.WriteDiags)
        Me.Controls.Add(Me.DiagFilename)
        Me.Controls.Add(Me.label1)
        Me.Controls.Add(Me.GetDongleInfo)
        Me.Name = "Form1"
        Me.Text = "ChangeTest"
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' Get Dongle Information
    Private Sub GetDongleInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetDongleInfo.Click
        Dim sType, sModel, sProdCode, sDisplay As String
        Dim ret_code, i, num_found As Integer
        ' our arrays
        Dim type(MAX_USB_DEVICES), model(MAX_USB_DEVICES), update_number(MAX_USB_DEVICES) As Integer
        Dim prodcode(MAX_USB_DEVICES * MAX_PRODCODE_LEN) As Byte
        Dim dongle_number(MAX_USB_DEVICES) As UInt32

        ' find dongle information for all dongle types, Plus & Net models, for all product codes
        ' NB In practice you will probably want to set the prodcode_mask to the value of your product code
        ret_code = DCGetInfo(TYPE_MASK_ALL, MODEL_MASK_DEFAULT, Nothing, MAX_USB_DEVICES, num_found, type, model, prodcode, dongle_number, update_number)

        ' check error code and display error if an error occurs
        If ret_code = 401 Then
            MsgBox("No dongles detected matching the search criteria specified", MsgBoxStyle.OkOnly, "Error")
            Exit Sub
        ElseIf ret_code <> 0 Then
            MsgBox("Error " + Str(ret_code) + " getting dongle information", MsgBoxStyle.OkOnly, "Error")
            Exit Sub
        End If

        ' display info from the dongles detected
        sDisplay = ""
        For i = 0 To num_found - 1
            ' get type of dongle
            If type(i) = TYPE_PRO Then
                sType = "Pro"
            Else
                sType = "FD"
            End If
            ' get model of dongle
            If model(i) = MODEL_LITE Then
                sModel = "Lite"
            ElseIf model(i) = MODEL_PLUS Then
                sModel = "Plus"
            Else
                sModel = "Net"
            End If
            ' get product code
            sProdCode = GetProductCode(prodcode, i)
            sDisplay = sDisplay + Str(i + 1) + ". Dinkey " + sType + " " + sModel + " with dongle number: " + dongle_number(i).ToString + ", product code: " + sProdCode + ", update number: " + Str(update_number(i)) + vbNewLine
        Next
        MsgBox(sDisplay, MsgBoxStyle.OkOnly, "Dongle Information")
    End Sub


    ' Write Dongle Diagnostics to file 
    Private Sub WriteDiags_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WriteDiags.Click
        Dim ret_code As Integer

        ret_code = DCGetDiagnosticInfo(DiagFilename.Text)

        ' check error code and display error if an error occurs
        If ret_code = 754 Then
            MsgBox("Could not create the diagnostic file. Please check that the path exists: " + DiagFilename.Text, MsgBoxStyle.OkOnly, "Error")
            Exit Sub
        ElseIf ret_code <> 0 Then
            MsgBox("Error " + Str(ret_code) + " writing diagnostic information", MsgBoxStyle.OkOnly, "Error")
            Exit Sub
        End If

        ' display success
        MsgBox("Diagnostic file written successfully.", MsgBoxStyle.OkOnly, "Dongle Information")
    End Sub


    ' Apply short update code entered manually
    Private Sub DoUpdateCodeString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DoUpdateCodeString.Click
        Dim ret_code, confirmation_code, extended_error As Integer

        ret_code = DCDoUpdateCodeString(UpdateCodeString.Text, confirmation_code, extended_error)

        ' check error code and display error if an error occurs
        If ret_code <> 0 Then
            DisplayError(ret_code, extended_error)
            Exit Sub
        End If

        ' display success
        MsgBox("Dongle updated successfully!" + vbNewLine + "Confirmation code is : " + Hex(confirmation_code), MsgBoxStyle.OkOnly, "Dongle Information")
    End Sub

    Private Sub DoUpdateCodeFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DoUpdateCodeFile.Click
        Dim ret_code, confirmation_code, extended_error As Integer

        ret_code = DCDoUpdateCodeFromFile(UpdateCodeFile.Text, confirmation_code, extended_error)

        ' check error code and display error if an error occurs
        If ret_code <> 0 Then
            DisplayError(ret_code, extended_error)
            Exit Sub
        End If

        ' display success
        MsgBox("Dongle updated successfully!" + vbNewLine + "Confirmation code is : " + Hex(confirmation_code), MsgBoxStyle.OkOnly, "Dongle Information")
    End Sub

    Private Sub RestoreFDLite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RestoreFDLite.Click
        Dim ret_code As Integer

        ret_code = DCRestoreDinkeyFDLite()

        ' check error code and display error if an error occurs
        If ret_code <> 0 Then
            DisplayError(ret_code, 0)
            Exit Sub
        End If

        ' display success
        MsgBox("Dinkey FD Lite updated successfully!", MsgBoxStyle.OKOnly, "Dongle Information")
    End Sub

    Private Sub GetMachineID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetMachineID.Click
        Dim ret_code, extended_error As Integer
        Dim machineID As UInt32

        ret_code = DCGetMachineID(machineID, extended_error)

        ' check error code and display error if an error occurs
        If ret_code <> 0 Then
            DisplayError(ret_code, extended_error)
            Exit Sub
        End If

        ' success - display the machineID
        ShowMachineID.Text = String.Format("{0:X8}", machineID)
    End Sub

    Private Sub DownloadTempSoftwareKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadTempSoftwareKey.Click
        Dim ret_code, extended_error As Integer
        Dim machineID As UInt32

        ' first get the machine ID
        ret_code = DCGetMachineID(machineID, extended_error)

        ' check error code and display error if an error occurs
        If ret_code <> 0 Then
            DisplayError(ret_code, extended_error)
            Exit Sub
        End If

        ' ...then try to download the temporary software key
		ret_code = DCDownloadTempSoftwareKey(machineID, extended_error)

        ' check error code and display error if an error occurs
        If ret_code <> 0 Then
            DisplayError(ret_code, extended_error)
            Exit Sub
        End If

        MsgBox("The Temporary Software Key has been downloaded successfully!", MsgBoxStyle.OKOnly, "Dongle Information")
    End Sub

    Private Sub DownloadDemoSoftwareKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadDemoSoftwareKey.Click
        Dim ret_code, extended_error As Integer
        Dim machineID As UInt32

        ' first get the machine ID
        ret_code = DCGetMachineID(machineID, extended_error)

        ' check error code and display error if an error occurs
        If ret_code <> 0 Then
            DisplayError(ret_code, extended_error)
            Exit Sub
        End If

        ' ...then try to download the demo software key
        ' (NB we are assuming only one dongle model is set in the Demo Software Template.
        ' If multiple models are set you will also need to specify the exact model)
		ret_code = DCDownloadDemoSoftwareKey(machineID, ProdCode.Text, SWKEY_MODEL_DEFAULT, extended_error)

        ' check error code and display error if an error occurs
        If ret_code <> 0 Then
            DisplayError(ret_code, extended_error)
            Exit Sub
        End If

        MsgBox("The Demo Software Key has been downloaded successfully!", MsgBoxStyle.OKOnly, "Dongle Information")
    End Sub
    
     ' look at the error code and try to display an appropriate message
    Private Sub DisplayError(ByVal ret_code As Integer, ByVal extended_error As Integer)
        Select Case ret_code
            Case 401
                MsgBox("Error! No dongles detected that meet the search criteria", MsgBoxStyle.OkOnly, "Error")
            Case 409
                MsgBox("Error! The dongle detected has not been programmed by DinkeyAdd.", MsgBoxStyle.OkOnly, "Error")
            Case 758
                MsgBox("Error! Cannot open the Update Code file specified.", MsgBoxStyle.OkOnly, "Error")
            Case 759
                MsgBox("Error! The file specified is not a valid Update Code file.", MsgBoxStyle.OkOnly, "Error")
            Case 762, 763, 764
                MsgBox("Error! Update Code is not in a correct format / update code file is corrupt.", MsgBoxStyle.OkOnly, "Error")
            Case 765
                MsgBox("Error! The Update Code does not match any dongle attached to your machine.", MsgBoxStyle.OkOnly, "Error")
            Case 766
                MsgBox("Error! The update number for this Update Code is too high. You need to first enter an update code that was previously sent to you.", MsgBoxStyle.OkOnly, "Error")
            Case 767
                MsgBox("Error! You have already entered this Update Code.", MsgBoxStyle.OkOnly, "Error")
            Case 1905
                MsgBox("Error! There is no Software Key available for download.", MsgBoxStyle.OkOnly, "Error")
            Case 1907
                MsgBox("Error! The Temporary Software Key has expired. Cannot download it.", MsgBoxStyle.OkOnly, "Error")
            Case 1910
                MsgBox("Error! The Software Key has already been downloaded.", MsgBoxStyle.OkOnly, "Error")
            Case 1921
                MsgBox("Error! You specified the 'default model' but there is more than one model available. You need to specify a specific dongle model.", MsgBoxStyle.OkOnly, "Error")
            Case 1922:
                MsgBox("Error! The model you requested is not available.", MsgBoxStyle.OkOnly, "Error") 
            Case 1923:
                MsgBox("Error! The Demo Software Key Template has been disabled.", MsgBoxStyle.OkOnly, "Error")
            Case 1924:
                MsgBox("Error! You have run out of Demo Software Key activations. You need to order some more.", MsgBoxStyle.OkOnly, "Error")
            Case Else
                MsgBox("An error occurred checking the dongle. Error: " + Str(ret_code) + ". Extended Error: " + Str(extended_error) + ".", MsgBoxStyle.OkOnly, "Error")
        End Select
    End Sub 
    
End Class
