' !! 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 marshal our structures in drpf.vb


Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Const AppName As String = "drpfTest"

#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 updatecodeMod As System.Windows.Forms.Button
    Friend WithEvents updatecode As System.Windows.Forms.Button
    Friend WithEvents drpfFile As System.Windows.Forms.TextBox
    Friend WithEvents label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.updatecodeMod = New System.Windows.Forms.Button
        Me.updatecode = New System.Windows.Forms.Button
        Me.drpfFile = New System.Windows.Forms.TextBox
        Me.label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'updatecodeMod
        '
        Me.updatecodeMod.Location = New System.Drawing.Point(88, 128)
        Me.updatecodeMod.Name = "updatecodeMod"
        Me.updatecodeMod.Size = New System.Drawing.Size(264, 23)
        Me.updatecodeMod.TabIndex = 7
        Me.updatecodeMod.Text = "Generate Update Code with modifications "
        '
        'updatecode
        '
        Me.updatecode.Location = New System.Drawing.Point(88, 88)
        Me.updatecode.Name = "updatecode"
        Me.updatecode.Size = New System.Drawing.Size(264, 23)
        Me.updatecode.TabIndex = 6
        Me.updatecode.Text = "Generate Update Code"
        '
        'drpfFile
        '
        Me.drpfFile.Location = New System.Drawing.Point(8, 40)
        Me.drpfFile.Name = "drpfFile"
        Me.drpfFile.Size = New System.Drawing.Size(424, 20)
        Me.drpfFile.TabIndex = 5
        Me.drpfFile.Text = ""
        '
        'label1
        '
        Me.label1.Location = New System.Drawing.Point(8, 24)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(440, 16)
        Me.label1.TabIndex = 4
        Me.label1.Text = "Full path of drpf file:"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(456, 174)
        Me.Controls.Add(Me.updatecodeMod)
        Me.Controls.Add(Me.updatecode)
        Me.Controls.Add(Me.drpfFile)
        Me.Controls.Add(Me.label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' The sample code contains 2 functions. 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 DRPF file. These are shown in the these two functions
    ' updatecode_Click and updatecodeMod_Click:
    '
    ' updatecode_Click: we load the DRPF file from disk and get a handle and use it "as is" using the API
    '                   call LoadDRPFFromFile. You can make some limited changes to overwrite the default
    '                   behaviour by using API flags.
    '
    ' updatecodeMod_Click: we load the DRPF file from disk and load into structures. We can then modify the
    '                      DRPF settings by modifying this structure. This method uses the API calls
    '                      ReadDRPFEx and LoadDRPF.


    Private Sub updatecode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updatecode.Click
        Dim drpfHandle As IntPtr
        Dim err_code As Integer
        Dim Outputs As New UPDATE_CODE_OUTPUTS

        ' load DRPF from File into memory
        drpfHandle = LoadDRPFFromFile(drpfFile.Text, err_code)
        If IntPtr.Zero.Equals(drpfHandle) Then
            MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, AppName)
            Exit Sub
        End If

        ' generate update code as specified by the DRPF file
        err_code = GenerateUpdateCode(drpfHandle, Convert.ToUInt32(0), 0, Nothing, Nothing, Outputs)

        ' Unload DRPF from memory
        UnloadDRPF(drpfHandle)

        ' 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 updatecodeMod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updatecodeMod.Click
        Dim drpfHandle As IntPtr
        Dim err_code, dummy1, dummy2 As Integer
        Dim MyRemoteInfo As REMOTEINFO
        Dim LicenceInfoArray() As LICENCEINFO
        Dim AlgChangesArray() As ALGCHANGE
        Dim DataChangesArray() As DATACHANGE
        Dim Outputs As New UPDATE_CODE_OUTPUTS

        ' in this case we need to read the DRPF into our structures, so that the DRPF values can be modified
        err_code = ReadDRPFEx(drpfFile.Text, MyRemoteInfo, LicenceInfoArray, AlgChangesArray, DataChangesArray, dummy1, dummy2)
        If err_code <> 0 Then
            MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, AppName)
            Exit Sub
        End If

        ' patch new values
        LicenceInfoArray(0).FeaturesFlag = 1                        ' so that the features value below will be recognised
        LicenceInfoArray(0).Features = Convert.ToUInt32(12345)
        LicenceInfoArray(0).AddSetExecs = 1                         ' so the executions will be added to existing
        LicenceInfoArray(0).Execs = 100

        ' get DRPF handle
        drpfHandle = LoadDRPF(MyRemoteInfo, LicenceInfoArray, AlgChangesArray, DataChangesArray, err_code)
        If IntPtr.Zero.Equals(drpfHandle) Then
            MsgBox(GetLastMessage(), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, AppName)
            Exit Sub
        End If

        ' generate update code as specified by the modified DRPF file
        err_code = GenerateUpdateCode(drpfHandle, Convert.ToUInt32(0), 0, Nothing, Nothing, Outputs)

        ' Unload DRPF from memory
        UnloadDRPF(drpfHandle)

        ' 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 restore warnings
#End If
End Class
