' !! 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.Runtime.InteropServices          ' so we can marshall our structures

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Const AppName As String = "dapfTest"
#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 groupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents label6 As System.Windows.Forms.Label
    Friend WithEvents maxDaysText As System.Windows.Forms.TextBox
    Friend WithEvents demoSoftwareKey As System.Windows.Forms.Button
    Friend WithEvents groupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents expyearText As System.Windows.Forms.TextBox
    Friend WithEvents label5 As System.Windows.Forms.Label
    Friend WithEvents expmonthText As System.Windows.Forms.TextBox
    Friend WithEvents label4 As System.Windows.Forms.Label
    Friend WithEvents expdayText As System.Windows.Forms.TextBox
    Friend WithEvents label3 As System.Windows.Forms.Label
    Friend WithEvents label2 As System.Windows.Forms.Label
    Friend WithEvents machineIDText As System.Windows.Forms.TextBox
    Friend WithEvents tempSoftwareKey As System.Windows.Forms.Button
    Friend WithEvents encryptShellData As System.Windows.Forms.Button
    Friend WithEvents lockSoftware As System.Windows.Forms.Button
    Friend WithEvents programDongleMod As System.Windows.Forms.Button
    Friend WithEvents programDongle As System.Windows.Forms.Button
    Friend WithEvents dapfFile As System.Windows.Forms.TextBox
    Friend WithEvents label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.groupBox2 = New System.Windows.Forms.GroupBox
        Me.label6 = New System.Windows.Forms.Label
        Me.maxDaysText = New System.Windows.Forms.TextBox
        Me.demoSoftwareKey = New System.Windows.Forms.Button
        Me.groupBox1 = New System.Windows.Forms.GroupBox
        Me.expyearText = New System.Windows.Forms.TextBox
        Me.label5 = New System.Windows.Forms.Label
        Me.expmonthText = New System.Windows.Forms.TextBox
        Me.label4 = New System.Windows.Forms.Label
        Me.expdayText = New System.Windows.Forms.TextBox
        Me.label3 = New System.Windows.Forms.Label
        Me.label2 = New System.Windows.Forms.Label
        Me.machineIDText = New System.Windows.Forms.TextBox
        Me.tempSoftwareKey = New System.Windows.Forms.Button
        Me.encryptShellData = New System.Windows.Forms.Button
        Me.lockSoftware = New System.Windows.Forms.Button
        Me.programDongleMod = New System.Windows.Forms.Button
        Me.programDongle = New System.Windows.Forms.Button
        Me.dapfFile = New System.Windows.Forms.TextBox
        Me.label1 = New System.Windows.Forms.Label
        Me.groupBox1.SuspendLayout()
        Me.SuspendLayout()       
        '
        'groupBox2
        '
        Me.groupBox2.Controls.Add(Me.label6)
        Me.groupBox2.Controls.Add(Me.maxDaysText)
        Me.groupBox2.Controls.Add(Me.demoSoftwareKey)
        Me.groupBox2.Location = New System.Drawing.Point(16, 390)
        Me.groupBox2.Name = "groupBox2"
        Me.groupBox2.Size = New System.Drawing.Size(336, 96)
        Me.groupBox2.TabIndex = 17
        Me.groupBox2.TabStop = False
        Me.groupBox2.Text = "Demo Software Key"
        '
        'demoSoftwareKey
        '
        Me.demoSoftwareKey.Location = New System.Drawing.Point(64, 56)
        Me.demoSoftwareKey.Name = "demoSoftwareKey"
        Me.demoSoftwareKey.Size = New System.Drawing.Size(208, 23)
        Me.demoSoftwareKey.TabIndex = 15
        Me.demoSoftwareKey.Text = "Create Demo Template"
        '
        'label6
        '
        Me.label6.Location = New System.Drawing.Point(24, 24)
        Me.label6.Name = "label6"
        Me.label6.Size = New System.Drawing.Size(100, 20)
        Me.label6.TabIndex = 18
        Me.label6.Text = "Max Days:"
        Me.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'maxDaysText
        '
        Me.maxDaysText.Location = New System.Drawing.Point(128, 24)
        Me.maxDaysText.Name = "maxDaysText"
        Me.maxDaysText.Size = New System.Drawing.Size(184, 20)
        Me.maxDaysText.TabIndex = 19
        Me.maxDaysText.Text = ""
        '
        'groupBox1
        '
        Me.groupBox1.Controls.Add(Me.expyearText)
        Me.groupBox1.Controls.Add(Me.label5)
        Me.groupBox1.Controls.Add(Me.expmonthText)
        Me.groupBox1.Controls.Add(Me.label4)
        Me.groupBox1.Controls.Add(Me.expdayText)
        Me.groupBox1.Controls.Add(Me.label3)
        Me.groupBox1.Controls.Add(Me.label2)
        Me.groupBox1.Controls.Add(Me.machineIDText)
        Me.groupBox1.Controls.Add(Me.tempSoftwareKey)
        Me.groupBox1.Location = New System.Drawing.Point(16, 251)
        Me.groupBox1.Name = "groupBox1"
        Me.groupBox1.Size = New System.Drawing.Size(336, 128)
        Me.groupBox1.TabIndex = 7
        Me.groupBox1.TabStop = False
        Me.groupBox1.Text = "Temporary Software Key"
        '
        'tempSoftwareKey
        '
        Me.tempSoftwareKey.Location = New System.Drawing.Point(64, 88)
        Me.tempSoftwareKey.Name = "tempSoftwareKey"
        Me.tempSoftwareKey.Size = New System.Drawing.Size(208, 23)
        Me.tempSoftwareKey.TabIndex = 16
        Me.tempSoftwareKey.Text = "Create Software Key"
        '
        'expyearText
        '
        Me.expyearText.Location = New System.Drawing.Point(240, 56)
        Me.expyearText.Name = "expyearText"
        Me.expyearText.Size = New System.Drawing.Size(32, 20)
        Me.expyearText.TabIndex = 15
        Me.expyearText.Text = "yyyy"
        '
        'label5
        '
        Me.label5.Location = New System.Drawing.Point(224, 56)
        Me.label5.Name = "label5"
        Me.label5.Size = New System.Drawing.Size(8, 20)
        Me.label5.TabIndex = 14
        Me.label5.Text = "/"
        Me.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'expmonthText
        '
        Me.expmonthText.Location = New System.Drawing.Point(184, 56)
        Me.expmonthText.Name = "expmonthText"
        Me.expmonthText.Size = New System.Drawing.Size(32, 20)
        Me.expmonthText.TabIndex = 13
        Me.expmonthText.Text = "mm"
        '
        'label4
        '
        Me.label4.Location = New System.Drawing.Point(168, 56)
        Me.label4.Name = "label4"
        Me.label4.Size = New System.Drawing.Size(8, 20)
        Me.label4.TabIndex = 12
        Me.label4.Text = "/"
        Me.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'expdayText
        '
        Me.expdayText.Location = New System.Drawing.Point(128, 56)
        Me.expdayText.Name = "expdayText"
        Me.expdayText.Size = New System.Drawing.Size(32, 20)
        Me.expdayText.TabIndex = 11
        Me.expdayText.Text = "dd"
        '
        'label3
        '
        Me.label3.Location = New System.Drawing.Point(24, 56)
        Me.label3.Name = "label3"
        Me.label3.Size = New System.Drawing.Size(100, 20)
        Me.label3.TabIndex = 10
        Me.label3.Text = "Expiry Date:"
        Me.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'label2
        '
        Me.label2.Location = New System.Drawing.Point(24, 24)
        Me.label2.Name = "label2"
        Me.label2.Size = New System.Drawing.Size(100, 20)
        Me.label2.TabIndex = 8
        Me.label2.Text = "Machine ID:"
        Me.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'machineIDText
        '
        Me.machineIDText.Location = New System.Drawing.Point(128, 24)
        Me.machineIDText.Name = "machineIDText"
        Me.machineIDText.Size = New System.Drawing.Size(184, 20)
        Me.machineIDText.TabIndex = 9
        Me.machineIDText.Text = ""
        '
        'encryptShellData
        '
        Me.encryptShellData.Location = New System.Drawing.Point(80, 203)
        Me.encryptShellData.Name = "encryptShellData"
        Me.encryptShellData.Size = New System.Drawing.Size(208, 23)
        Me.encryptShellData.TabIndex = 6
        Me.encryptShellData.Text = "Encrypt Shell Data Files"
        '
        'lockSoftware
        '
        Me.lockSoftware.Location = New System.Drawing.Point(80, 163)
        Me.lockSoftware.Name = "lockSoftware"
        Me.lockSoftware.Size = New System.Drawing.Size(208, 23)
        Me.lockSoftware.TabIndex = 5
        Me.lockSoftware.Text = "Lock Software"
        '
        'programDongleMod
        '
        Me.programDongleMod.Location = New System.Drawing.Point(80, 123)
        Me.programDongleMod.Name = "programDongleMod"
        Me.programDongleMod.Size = New System.Drawing.Size(208, 23)
        Me.programDongleMod.TabIndex = 4
        Me.programDongleMod.Text = "Program Dongle with modification"
        '
        'programDongle
        '
        Me.programDongle.Location = New System.Drawing.Point(80, 83)
        Me.programDongle.Name = "programDongle"
        Me.programDongle.Size = New System.Drawing.Size(208, 23)
        Me.programDongle.TabIndex = 3
        Me.programDongle.Text = "Program Dongle"
        '
        'dapfFile
        '
        Me.dapfFile.Location = New System.Drawing.Point(16, 35)
        Me.dapfFile.Name = "dapfFile"
        Me.dapfFile.Size = New System.Drawing.Size(344, 20)
        Me.dapfFile.TabIndex = 2
        Me.dapfFile.Text = ""
        '
        'label1
        '
        Me.label1.Location = New System.Drawing.Point(16, 19)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(416, 16)
        Me.label1.TabIndex = 1
        Me.label1.Text = "Full path of dapf file:"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(376, 496)
        Me.Controls.Add(Me.groupBox2)
        Me.Controls.Add(Me.groupBox1)
        Me.Controls.Add(Me.encryptShellData)
        Me.Controls.Add(Me.lockSoftware)
        Me.Controls.Add(Me.programDongleMod)
        Me.Controls.Add(Me.programDongle)
        Me.Controls.Add(Me.dapfFile)
        Me.Controls.Add(Me.label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.groupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    '   The sample code contains 5 functions. You will not need to use all these functions in your code.
    '   Just use which ever functions are most appropriate and customise in your own way. The sample code
    '   is just a guide.  
    '
    '   There are two methods to loading a DAPF file. These are shown in the first two functions
    '   programDongle_Click and programDongleMod_Click:
    '
    '   programDongle_Click: we load the DAPF file from disk and get a handle and use it "as is" using the API
    '                          call LoadDAPFFromFile. You can make some limited changes to overwrite the default
    '                         behaviour by using API flags.
    '
    '   programDongleMod_Click: we load the DAPF file from disk and load into structures. We can then modify the
    '                          DAPF settings by modifying this structure. This method uses the API calls
    '                         ReadDAPFEx and LoadDAPF.
    '
    '   The other functions use the first method (as it is simpler) but you can use the second method instead if
    '   you prefer.
    '
    '   Note - an alternative to using the programDongleMod_Click technique is to modify the DAPF file on disk
    '   and then use the programDongle_Click technique. To modify a binary DAPF you would use the binary structures
    '   that are defined in dapf.h. To modify JSON DAPF you would need to use a JSON parser.
    '
    '   lockSoftware_Click : locks software (protect it) to a dongle as specified by the DAPF file.
    '
    '   encryptShellData_Click: encrypts the shell data files specified in the DAPF file (this is used with encrypting
    '                          data file if you use the Shell Method with "Data File Encryption" enabled).
    '
    '   tempSoftwareKey_Click: creates a Temporary Software Key based on the DAPF file and settings the user has entered. 
    '
    '   demoSoftwareKey_Click: creates a Demo Software Key Template based on the DAPF file and settings the user has entered. 

    Private Sub programDongle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles programDongle.Click
        Dim dapfHandle As IntPtr
        Dim err_code As Integer
        Dim dongle_number As UInt32

        ' load DAPF from File into memory
        dapfHandle = LoadDAPFFromFile(dapfFile.Text, err_code)
        If IntPtr.Zero.Equals(dapfHandle) Then
            MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, AppName)
            Exit Sub
        End If

        ' program dongle as specified by the DAPF file
        err_code = AddProtection(dapfHandle, ACTION_PROGRAM_DONGLE, 0, 0, dongle_number)

        ' Unload DAPF from memory
        UnloadDAPF(dapfHandle)

        ' display message (error or success)
        MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, AppName)
    End Sub
#If VBC_VER >= 14 Then
    #Disable Warning BC42108, BC42030					'This line is used in Visual Studio 2015 (and newer) to remove warnings in this function
#End If
    Private Sub programDongleMod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles programDongleMod.Click
        Dim dapfHandle As IntPtr
        Dim err_code, dummy1, dummy2 As Integer
        Dim dongle_number As UInt32
        Dim MyDinkeyInfo As DINKEYINFO
        Dim LicenceInfoArray() As LICENCEINFO
        Dim FileInfoArray() As FILEINFO
        Dim AlgorithmArray() As ALGORITHM
        Dim DataFileEncArray() As DATAFILEENC

        ' in this case we need to read the DAPF into our structures, so that the DAPF values can be modified
        err_code = ReadDAPFEx(dapfFile.Text, MyDinkeyInfo, LicenceInfoArray, FileInfoArray, AlgorithmArray, DataFileEncArray, dummy1, dummy2)
        If err_code <> 0 Then
            MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, AppName)
            Exit Sub
        End If

        ' patch new values
        LicenceInfoArray(0).Features = Convert.ToUInt32(12345)
        LicenceInfoArray(0).Execs = 100
        LicenceInfoArray(0).LicenceName = "fred"

        ' get DAPF handle
        dapfHandle = LoadDAPF(MyDinkeyInfo, LicenceInfoArray, FileInfoArray, AlgorithmArray, DataFileEncArray, err_code)
        If IntPtr.Zero.Equals(dapfHandle) Then
            MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, AppName)
            Exit Sub
        End If

        ' program dongle
        err_code = AddProtection(dapfHandle, ACTION_PROGRAM_DONGLE, 0, 0, dongle_number)

        ' Unload DAPF from memory
        UnloadDAPF(dapfHandle)

        ' display message (error or success)
        MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, AppName)
    End Sub
#If VBC_VER >= 14 Then
    #Enable Warning BC42108, BC42030				'This line is used in Visual Studio 2015 (and higher) to remove warnings
#End If
    ' load a DAPF file from disk and protect the files specified
    ' (in practice it is better to protect the files one at a time and output messages as you go along, similar to DinkeyAdd.exe)
    ' In this example we specify the flag to overwrite any existing output file that might exist.
    Private Sub lockSoftware_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lockSoftware.Click
        Dim dapfHandle As IntPtr
        Dim err_code As Integer
        Dim dummy As UInt32

        ' load DAPF from File into memory
        dapfHandle = LoadDAPFFromFile(dapfFile.Text, err_code)
        If IntPtr.Zero.Equals(dapfHandle) Then
            MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, AppName)
            Exit Sub
        End If

        ' protect all the files listed in the DAPF file
        err_code = AddProtection(dapfHandle, ACTION_PROTECT_SOFTWARE, -1, FLAGS_OVERWRITE_OUTPUT, dummy)

        ' Unload DAPF from memory
        UnloadDAPF(dapfHandle)

        ' display message (error or success)
        MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, AppName)
    End Sub

    Private Sub encryptShellData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles encryptShellData.Click
        Dim dapfHandle As IntPtr
        Dim err_code As Integer

        ' load DAPF from File into memory
        dapfHandle = LoadDAPFFromFile(dapfFile.Text, err_code)
        If IntPtr.Zero.Equals(dapfHandle) Then
            MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, AppName)
            Exit Sub
        End If

        ' encrypt all the shell data files listed in the DAPF file
        err_code = EncryptShellDataFiles(dapfHandle, -1, FLAGS_OVERWRITE_OUTPUT)

        ' Unload DAPF from memory
        UnloadDAPF(dapfHandle)

        ' display message (error or success)
        MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, AppName)
    End Sub

    Private Sub tempSoftwareKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tempSoftwareKey.Click
        Dim dapfHandle As IntPtr
        Dim err_code As Integer
        Dim swkey_info As TEMP_SWKEY_INFO

        ' load DAPF from File into memory
        dapfHandle = LoadDAPFFromFile(dapfFile.Text, err_code)
        If IntPtr.Zero.Equals(dapfHandle) Then
            MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, AppName)
            Exit Sub
        End If

        ' fill out the SWKEY structure from the values the user entered
        swkey_info.Size = Marshal.SizeOf(swkey_info.GetType)
        swkey_info.MachineID = Convert.ToUInt32(machineIDText.Text, 16)
        swkey_info.ExpiryDay = Convert.ToInt32(expdayText.Text, 10)
        swkey_info.ExpiryMonth = Convert.ToInt32(expmonthText.Text, 10)
        swkey_info.ExpiryYear = Convert.ToInt32(expyearText.Text, 10)
        ' !!!! in this case we just choose Pro Plus. You could load the DAPF file into structures and see which model is selected.
        ' If there is more than one model selected you can ask the customer to choose which model they want.
        swkey_info.DongleModel = SWKEY_MODEL_PRO_PLUS
        swkey_info.DongleNumber = Convert.ToUInt32(0)

        ' attempt to create the Temporary Software Key
        err_code = CreateTempSoftwareKey(dapfHandle, swkey_info, 0)

        ' Unload DAPF from memory
        UnloadDAPF(dapfHandle)

        ' display message (error or success)
        MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, AppName)
    End Sub
    
    Private Sub demoSoftwareKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles demoSoftwareKey.Click
        Dim dapfHandle As IntPtr
        Dim err_code As Integer
        Dim swkey_info As DEMO_SWKEY_INFO

        ' load DAPF from File into memory
        dapfHandle = LoadDAPFFromFile(dapfFile.Text, err_code)
        If IntPtr.Zero.Equals(dapfHandle) Then
            MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, AppName)
            Exit Sub
        End If

        ' fill out the SWKEY structure from the values the user entered
        swkey_info.Size = Marshal.SizeOf(swkey_info.GetType)
        swkey_info.MaxDays = Convert.ToInt32(maxDaysText.Text, 10)
        ' !!!! in this case we just choose the default model (assuming you only selected one in the DAPF file).
        ' If you want to specify more than one model then OR the flags together. e.g. SWKEY_BITMAP_PRO_LITE Or SWKEY_BITMAP_PRO_PLUS
        swkey_info.Models = SWKEY_BITMAP_DEFAULT

        ' attempt to create the Demo Software Key Template
        err_code = CreateDemoSoftwareKeyTemplate(dapfHandle, swkey_info, 0)

        ' Unload DAPF from memory
        UnloadDAPF(dapfHandle)

        ' display message (error or success)
        MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, AppName)
    End Sub    
End Class
