Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Dongle Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Dongle/UniKey
This example is the version from Sun, 23th Sep 2017.
Project "UniKey.xojo_binary_project"
FileTypes
Filetype text
End FileTypes
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
PushButton1.Enabled=false
PushButton2.Enabled=false
test
PushButton1.Enabled=true
PushButton2.Enabled=true
End EventHandler
End Control
Control ListBox1 Inherits ListBox
ControlInstance ListBox1 Inherits ListBox
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
PushButton1.Enabled=false
PushButton2.Enabled=false
w=new WorkThread
w.run
End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Sub run100Tests()
dim i as integer
for i=1 to 100
StaticText1.text=str(i)+" of 100"
test
next
PushButton1.Enabled=true
PushButton2.Enabled=true
End Sub
Protected Sub test()
ListBox1.DeleteAllRows
'Sample for UniKey API
Const UNIKEY_FIND = 1 ' Find UniKey
Const UNIKEY_FIND_NEXT = 2 ' Find next UniKey
Const UNIKEY_LOGON = 3 ' Open UniKey
Const UNIKEY_LOGOFF = 4 ' Close UniKey
Const UNIKEY_READ_MEMORY = 5 ' Read UniKey
Const UNIKEY_WRITE_MEMORY = 6 ' Write UniKey
Const UNIKEY_RANDOM = 7 ' Generate Random Number
Const UNIKEY_SEED = 8 ' Generate Seed Code
Const UNIKEY_WRITE_SOFTID = 9 ' Write Software ID
Const UNIKEY_READ_SOFTID = 10 ' Read Software ID
Const UNIKEY_SET_MODULE = 11 ' Set license module
Const UNIKEY_CHECK_MODULE = 12 ' Check license module
Const UNIKEY_WRITE_ARITHMETIC = 13 ' Write Arithmetic
Const UNIKEY_CALCULATE1 = 14 ' Calculate 1
Const UNIKEY_CALCULATE2 = 15 ' Calculate 2
Const UNIKEY_CALCULATE3 = 16 ' Calculate 3
Const UNIKEY_MODULE_DECRASE = 17 ' Decrease license module Unit
Const UNIKEY_SET_NEW_PASSWORD = 18 ' Set New Password via a New Seed
Const UNIKEY_GENERATE_KEY = 19 ' Generate a New Key in specific Key Store
Const UNIKEY_ENCRYPT = 20 ' Encrypt the u.buffer with a Specific Key
Const UNIKEY_DECRYPT = 21 ' Decrypt the u.buffer with a Specific Key
Const UNIKEY_MD5 = 22 ' Make an MD5 Digest for a Certain Content
Const UNIKEY_READ_UPDATETAG = 23 ' Read UpdateTag from a Specific Key
Const UNIKEY_WRITE_UPDATETAG = 24 ' Write UpdateTag to a Specific Key
'Error Code
Const SUCCESS = 0 'Success
Const ERROR_NO_UNIKEY = 3 'No UniKey dongle
Const ERROR_INVALID_PASSWORD = 4 'Found UniKey dongle, but Basic passwords are incorrect
Const ERROR_INVALID_PASSWORD_OR_ID = 5 'Wrong password or UniKey HID
Const ERROR_SET_ID = 6 'Set UniKey ID wrong
Const ERROR_INVALID_ADDR_OR_SIZE = 7 'Read/Write address or length is wrong
Const ERROR_UNKNOWN_COMMAND = 8 'No such command
Const ERROR_NOTBELEVEL3 = 9 'Internal Error
Const ERROR_READ = 10 'Read Error
Const ERROR_WRITE = 11 'Write Error
Const ERROR_RANDOM = 12 'Random number Error
Const ERROR_SEED = 13 'Seed code Error
Const ERROR_CALCULATE = 14 'Calculate Error
Const ERROR_NO_OPEN = 15 'No open dongle before operating dongle
Const ERROR_OPEN_OVERFLOW = 16 'Too many open dongles (>16)
Const ERROR_NOMORE = 17 'No more dongle
Const ERROR_NEED_FIND = 18 'No Find before FindNext
Const ERROR_MODULE_DECRASE = 19 'MODULE_DECRASE Error
Const ERROR_AR_BAD_COMMAND = 20 'Arithmetic instruction Error
Const ERROR_AR_UNKNOWN_OPCODE = 21 'Arithmetic operator Error
Const ERROR_AR_WRON_GBEGIN = 22 'Const number can't use on first arithmetic instruction
Const ERROR_AR_WRONG_END = 23 'Const number can't use on last arithmetic instruction
Const ERROR_AR_VALUE_OVERFLOW = 24 'Const number > 63
Const ERROR_TOO_MUCH_THREAD = 25 'Too many (>100) threads in the single process open the dongle
Const ERROR_INVALID_KEY = 26 'The key in the key storage is wrong
Const ERROR_VERIFY_ADV_PASSWORD = 27 'Advanced passwords (password3 and passowrd4) not verified
Const ERROR_INVALID_KEY_STORE = 28 'Wrong key storage number
Const EER_GENERATE_NEW_PASSWORD = 29 'Generate new password Error
Const ERROR_READ_UPDATETAG = 30 'Read UpdateTag Error
Const ERROR_WRITE_UPDATETAG = 31 'Write UpdateTag Error
Const ERROR_RECEIVE_NULL = 256 'Receive null
Const ERROR_UNKNOWN_SYSTEM = 258 'Unknown operating system
Const ERROR_UNKNOWN = 65535 'Unknown Error
'Declare Function UniKey Lib "UniKey.DLL" (ByVal fun As Integer, ByRef handle As Integer, ByRef u.lp1 As Integer, ByRef u.lp2 As Integer, ByRef p1 As Integer, ByRef p2 As Integer, ByRef p3 As Integer, ByRef p4 As Integer, ByVal u.buffer As Ptr) As Integer
'Declare Function UniKey Lib "UniKey.dll" () as integer
', ByVal handle As Integer, ByRef u.lp1 As Integer, ByRef u.lp2 AsInteger, ByRef p1 As Integer, ByRef p2 As Integer, ByRef p3 As Integer, ByRef p4 As Integer, ByVal u.buffer As String
count=count+1
listbox1.addrow("UniKey Xojo Sample")
Dim i, j, retcode As Integer
Dim cAlg as String
dim u as new UnikeyMBS
u.p1 = 1234 'passwords
u.p2 = 1234
u.p3 = 1234
u.p4 = 1234
'find dongle
u.buffer= NewMemoryBlock(4096) // clear buffer
retcode = u.Unikey(UniKey_FIND)
If (retcode <> 0) Then
ListBox1.addrow ("Error Code:" + str( retcode))
errors=errors+1
else
' return the hardware ID (HID)
ListBox1.addrow ("Find dongle:" + str( u.lp1))
end if
'open dongle
retcode = u.Unikey(UNIKEY_LOGON)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow("Logon dongle:" + str(u.lp1))
end if
u.p1 = 3
u.p2 = 5
u.buffer.CString(0) = "Hello"
'write memory
retcode = u.Unikey(UNIKEY_WRITE_MEMORY)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Write: " + u.buffer.Cstring(0))
end if
'read dongle memory
u.p1 = 3
u.p2 = 5
u.buffer= NewMemoryBlock(4096) // clear buffer
retcode = u.Unikey(UNIKEY_READ_MEMORY)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Read: " + u.buffer.Cstring(0))
end if
'random generation function
retcode = u.Unikey(UNIKEY_RANDOM)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
Exit
End If
ListBox1.addrow ( "Random:" + str(u.p1))
'seed function, you can use either str or dec
u.lp2 = &H12345678
retcode = u.Unikey(UNIKEY_SEED)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Seed: " + str(u.p1) + " " + str(u.p2) + " " + str(u.p3) + " " + str(u.p4))
end if
'write SoftID
u.lp1 = 88888888
retcode = u.Unikey(UNIKEY_WRITE_SOFTID)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Write User ID: " + str(u.lp1))
end if
'read SoftID
u.lp1 = 0
retcode = u.Unikey(UNIKEY_READ_SOFTID)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Read User ID: " +str(u.lp1))
end if
'set license module
u.p1 = 7
u.p2 = 2121
u.p3 = 0
retcode = u.Unikey(UNIKEY_SET_MODULE)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Set MODULE 7: Pass = " + str(u.p2) + " Decrease Not Allow")
end if
'check license module
u.p1 = 7
retcode = u.Unikey(UNIKEY_CHECK_MODULE)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Check MODULE 7: ")
If (u.p2 = 1) Then
ListBox1.addrow ( "Valid ")
Else
ListBox1.addrow ( "Invalid")
End If
If (u.p3 = 1) Then
ListBox1.addrow ( "Allow Decrease")
Else
ListBox1.addrow ( "Not Allow Decrease")
End If
end if
'write user-defined algorithm
u.p1 = 0
u.buffer=NewMemoryBlock(4096) // clear buffer
u.buffer.CString(0)="H=H^H, A=A*23, F=B*17, A=A+F, A=A+G, A=A<C, A=A^D, B=B^B, C=C^C, D=D^D"
retcode = u.Unikey(UNIKEY_WRITE_ARITHMETIC)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
Exit
End If
ListBox1.addrow ( "Write Arithmetic 1")
'calculation 1
u.lp1 = 0
u.lp2 = 7
u.p1 = 5
u.p2 = 3
u.p3 = 1
u.p4 =65535
retcode = u.Unikey(UNIKEY_CALCULATE1)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Calculate Input: p1=5, p2=3, p3=1, p4=65535")
ListBox1.addrow ( "Result = ((5*23 + 3*17 + &2121) < 1) ^ FFFF = BC71")
ListBox1.addrow ( "Calculate Output: p1=" + str(u.p1) + " p2=" + str(u.p2) + " p3= " + str(u.p3) + " p4 =" + str(u.p4))
end if
'write user-defined algorithm
u.p1 = 10
u.buffer=NewMemoryBlock(4096) // clear buffer
u.buffer.CString(0) = "A=A+B, A=A+C, A=A+D, A=A+E, A=A+F, A=A+G, A=A+H"
'u.buffer.byte(51)=0
retcode = u.Unikey(UNIKEY_WRITE_ARITHMETIC)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Write Arithmetic 2")
end if
'calculation2
u.lp1 = 10
u.lp2 = 1234
u.p1 = 1
u.p2 = 2
u.p3 = 3
u.p4 = 4
retcode = u.Unikey(UNIKEY_CALCULATE2)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Calculate Output: p1=" + str(u.p1) + " p2=" + str(u.p2) + " p3=" + str(u.p3) + " p4=" + str(u.p4))
end if
'Set License Module Decrease
u.p1 = 9
u.p2 = 5
u.p3 = 1
retcode = u.Unikey(UNIKEY_SET_MODULE)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
End If
'write user-defined algorithm
u.p1 = 17
u.buffer=NewMemoryBlock(4096) // clear buffer
u.buffer.CString(0) = "A=E|E, B=F|F, C=G|G, D=H|H"
'u.buffer.byte(30)=0
retcode = u.Unikey(UNIKEY_WRITE_ARITHMETIC)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Write Arithmetic 3")
end if
'Calculation3
u.lp1 = 17
u.lp2 = 6
u.p1 = 1
u.p2 = 2
u.p3 = 3
u.p4 = 4
retcode = u.Unikey(UNIKEY_CALCULATE3)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Show Module from 6: p1=" + str(u.p1) + " p2=" + str(u.p2) + " p3=" + str(u.p3) + " p4=" + str(u.p4))
end if
'Decrease License Module
u.p1 = 9
retcode = u.Unikey(UNIKEY_MODULE_DECRASE)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
Exit
End If
ListBox1.addrow ( "Decrease module 9")
' Calculation3
u.lp1 = 17
u.lp2 = 6
u.p1 = 1
u.p2 = 2
u.p3 = 3
u.p4 = 4
retcode = u.Unikey(UNIKEY_CALCULATE3)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "Show Module from 6: p1=" + str(u.p1) + " p2=" + str(u.p2) + " p3=" + str(u.p3) + " p4=" + str(u.p4))
end if
' generate new password
' if you really want to change password, please de-comment these codes
' u.lp1=1234;
' retcode=u.Unikey(UNIKEY_SET_NEW_PASSWORD,handle)
' if (retcode<>0) Then
' ListBox1.addrow ( "Error Code:" +str( retcode))
' else
' ListBox1.addrow ("Generate new passwords, seed:" + u.lp1 + " p1:" + p1 + " p2:" + p2 + " p3:" + p3 + " p4:" + p4
' end if
' generate key
u.lp1=1
retcode=u.Unikey(UNIKEY_GENERATE_KEY)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "generate key %d successed, key :" + str(u.lp1))
end if
' encrypt
u.buffer=NewMemoryBlock(4096) // clear buffer
u.buffer.CString(0)="abcdefghijklmnop"
u.lp1=16
u.lp2=1
retcode=u.Unikey(UNIKEY_ENCRYPT)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "encrypt successed, result: " + u.buffer.Cstring(0))
end if
' decrypt
u.lp1=16
u.lp2=1
retcode=u.Unikey(UNIKEY_DECRYPT)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "decrypt successed, result: " + u.buffer.Cstring(0))
end if
' write UpdateTag
u.lp1=1234
retcode=u.Unikey(UNIKEY_WRITE_UPDATETAG)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ( "write UpdateTag: " + str(u.lp1))
end if
' read UpdateTag
u.lp1=0
retcode=u.Unikey(UNIKEY_READ_UPDATETAG)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addrow ("read UpdateTag: " + str(u.lp1 ))
end if
' MD5
u.buffer=NewMemoryBlock(4096) // clear buffer
u.buffer.CString(0)="abcdefghijklmnop "
u.lp1=16
u.lp2=0
retcode=u.Unikey(UNIKEY_MD5)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
Exit
End If
ListBox1.addrow ( "MD5 successed, result: "+u.buffer.Cstring(0))
' close dongle
retcode = u.Unikey(UNIKEY_LOGOFF)
If (retcode <> 0) Then
ListBox1.addrow ( "Error Code:" +str( retcode))
else
ListBox1.addRow("Logoff dongle")
end if
StaticText2.text=str(errors)+" of "+str(count)
End Sub
Property count As integer
Property errors As integer
Property w As workthread
End Class
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "E&xit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
EventHandler Sub Open()
if TargetMacOS then
if TargetMachO then
'ok
else
MsgBox "Please use MachO target. PEF Is not supported."
end if
end if
End EventHandler
End Class
Class WorkThread Inherits Thread
EventHandler Sub Run()
window1.run100tests
End EventHandler
End Class
End Project
See also:
The items on this page are in the following plugins: MBS Dongle Plugin.