VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   4590
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7875
   LinkTopic       =   "Form1"
   ScaleHeight     =   4590
   ScaleWidth      =   7875
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton TestBtn 
      Caption         =   "Test"
      Height          =   375
      Left            =   3000
      TabIndex        =   1
      Top             =   4080
      Width           =   1575
   End
   Begin VB.ListBox List1 
      Height          =   3765
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   7575
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' ==========================================================================
' Demo: Write and run exe-file.
' Caution: no error check for the sake of briefness and understandability
' Tips:
' (1) You can write exe-files to dongle and run it in dongle. We have Integrate
'     Design Entironment(IDE), you can write,debug,and run program there.
' (2) dist.c is dongle program，we convert the compile result to bin file, and
'     put it to PC program via array.
' (3) You must write exe-file to root directory after super password verification.
' (4) Demo inclue 2 steps: write and run exe-file. The first step should finished befor
'     dongle vending. Then only second step is in final product.
' ==========================================================================
' dist.c caculate a^2+b^2
'
'void main()
'{
'  unsigned int a,b,c;
'  get_input(&a,0,1,1);
'  get_input(&b,2,1,1);
'  a=a*a;
'  b=b*b;
'  c=a+b;
'  set_response(2,&c);
'  exit();
'}
' ==========================================================================

Private Sub TestBtn_Click()
Dim errcode, num, hic, i As Long
Dim cmddata(0 To 255) As Byte ' command buffer
Dim buffer(0 To 255) As Byte ' user buffer
Dim byTmp As Byte
Dim curline As Integer

' convert from dist.bin 
Const DIST_SIZE = 193
Dim g_progDIST(0 To DIST_SIZE - 1) As Byte
Dim pos As Long
Dim varArray() As Variant














pos = 0
varArray = Array(&H2, &H0, &HB1, &H75, &HE, &H0, &H75, &HF, &H0, &H75, &H10, &H8, &HE4, &HF5, &H11, &H75)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&H12, &H1, &H75, &H13, &H1, &H12, &H0, &H69, &H75, &HE, &H0, &H75, &HF, &H0, &H75, &H10)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&HA, &H75, &H11, &H2, &H75, &H12, &H1, &H75, &H13, &H1, &H12, &H0, &H69, &HAE, &H8, &HAF)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&H9, &HAC, &H8, &HAD, &H9, &H12, &H0, &H9F, &H8E, &H8, &H8F, &H9, &HAE, &HA, &HAF, &HB)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&HAC, &HA, &HAD, &HB, &H12, &H0, &H9F, &H8E, &HA, &H8F, &HB, &HE5, &H9, &H25, &HB, &HF5)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&HD, &HE5, &H8, &H35, &HA, &HF5, &HC, &H75, &HE, &H2, &H75, &HF, &H0, &H75, &H10, &H0)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&H75, &H11, &HC, &H12, &H0, &H88, &H2, &H0, &HBD, &H12, &HFF, &H0, &HE5, &HE, &H24, &H0)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&HE5, &HF, &H24, &H1, &HE5, &H10, &H24, &H2, &HE5, &H11, &H24, &H3, &HE5, &H12, &H24, &H4)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&HE5, &H13, &H24, &H5, &H12, &HFF, &HB, &H22, &H12, &HFF, &H0, &HE5, &HF, &H24, &H0, &HE5)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&H10, &H24, &H1, &HE5, &H11, &H24, &H2, &HE5, &HE, &H24, &H3, &H12, &HFF, &HA, &H22, &HEF)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&H8D, &HF0, &HA4, &HA8, &HF0, &HCF, &H8C, &HF0, &HA4, &H28, &HCE, &H8D, &HF0, &HA4, &H2E, &HFE)
pos = ArrayByte(varArray, pos, g_progDIST)
varArray = Array(&H22, &H78, &H7F, &HE4, &HF6, &HD8, &HFD, &H75, &H81, &H13, &H2, &H0, &H3, &H12, &HFF, &HD, &H22)
pos = ArrayByte(varArray, pos, g_progDIST)


curline = 0
num = DIC_Find()
For i = 0 To num - 1
    hic = DIC_Open(i, 0&)
    If (hic >= 0) Then
        Exit For
    End If
Next i
If (i = num) Then
    Exit Sub
End If

' a. 提升权限为超级用户(创建可执行文件必须是超级用户才可以)
For i = 0 To 7
    cmddata(i) = 255
Next i
errcode = DIC_Command(hic, CHECK_SUPER_PASS, cmddata(0))

' b. 创建可执行文件
errcode = DIC_Set(cmddata(0), FILL, 256, 0, 0&)   ' 清空
errcode = DIC_Set(cmddata(0), FILE_ID, BY_VALUE, &H1, 0&)                     ' 文件 ID
errcode = DIC_Set(cmddata(0), FILE_CLASS, BY_VALUE, &HFF, 0&)                 ' 文件类别
errcode = DIC_Set(cmddata(0), FILE_ATTRIBUTE, BY_VALUE, FILEATTR_EXEC, 0&)    ' 文件属性
errcode = DIC_Set(cmddata(0), FILE_SIZE, BY_VALUE, DIST_SIZE, 0&)             ' 文件大小
Call Str2Array("6002", buffer)
errcode = DIC_Set(cmddata(0), FILE_NAME, BY_ARRAY, 0, buffer(0))
errcode = DIC_Command(hic, CREATE_FILE, cmddata(0))

' c. 写入可执行文件
errcode = DIC_Set(cmddata(0), WRITE_DATA, DIST_SIZE, 0, g_progDIST(0))
errcode = DIC_Command(hic, WRITE_FILE, cmddata(0))

' d. 把超级密码验证状态降低为普通用户状态，运行不需要超级用户来做了
'    而且在超级用户状态下所有的文件都被当作数据文件处理，无法运行程序
errcode = DIC_Close(hic)
errcode = DIC_Open(hic, 0&)

' e. 运行我们写入的文件
a = 3
b = 4
errcode = DIC_Set(buffer(0), 0, 2, a, 0&) ' 准备运行数据 a
errcode = DIC_Set(buffer(0), 2, 2, b, 0&) ' 准备运行数据 b
errcode = DIC_Set(cmddata(0), RUN_DATA, 4, &H6002, buffer(0))  ' 设置运行数据
errcode = DIC_Command(hic, RUN, cmddata(0))
errcode = DIC_Get(cmddata(0), RUN_DATA, BY_VALUE, buffer(0))   ' 取回运行后的数据

'由于锁内返回数据的字节序与PC上的不一致，这里要进行字节序转换。
'因为返回2个字节，所以颠倒buffer的前两个字节
byTmp = buffer(0)
buffer(0) = buffer(1)
buffer(1) = byTmp

c = DIC_Get(buffer(0), 0, BY_VALUE + 2, 0&)     ' 从运行后的数据中取回 c
List1.List(curline) = str(a) + "*" + str(a) + "+" + str(b) + "*" + str(b) + "=" + str(c)
curline = curline + 1

errcode = DIC_Close(hic)
End Sub

