Attribute VB_Name = "Module1"

' Interface:
'(1) Find Dongle
'    Input:
'    function = 0
'    p1 = pass1
'    p2 = pass2
'    p3 = pass3
'    p4 = pass4
'    Return:
'    lp1 = Rockey HID
'    return 0 = Success, else is error code
'
'(2) Find Next Dongle
'    Input:
'    function = 1
'    p1 = pass1
'    p2 = pass2
'    p3 = pass3
'    p4 = pass4
'    Return:
'    lp1 = Rockey HID
'    return 0 = Success, else is error code
'
'(3) Open Dongle
'    Input:
'    function = 2
'    p1 = pass1
'    p2 = pass2
'    p3 = pass3
'    p4 = pass4
'    lp1 = Rockey HID
'    Return:
'    handle = Opened dongle handle
'    return 0 = Success, else is error code
'
'(4) Cloase Dongle
'    Input:
'    function = 3
'    handle = dongle handle
'    Return:
'    return 0 = Success, else is error code
'
'(5) Read Dongle
'    Input:
'    function = 4
'    handle = dongle handle
'    p1 = pos
'    p2 = length
'    buffer = pointer of buffer
'    Return:
'    Fill buffer with read contents
'    return 0 = Success, else is error code
'
'(6) Write Dongle
'    Input:
'    function = 5
'    handle = dongle handle
'    p1 = pos
'    p2 = length
'    buffer = pointer of buffer
'    Return:
'    return 0 = Success, else is error code
'
'(7) Generate Random Number
'    Input:
'    function = 6
'    handle = dongle handle
'    Return:
'    p1 = random number
'    return 0 = Success, else is error code
'
'(8) Generate Seed Code
'    Input:
'    function = 7
'    handle = dongle handle
'    lp2 = seed code
'    Return:
'    p1 = seed return code 1
'    p2 = seed return code 2
'    p3 = seed return code 3
'    p4 = seed return code 4
'    return 0 = Success, else is error code
'
'(9) Write User ID [*]
'    Input:
'    function = 8
'    handle = dongle handle
'    lp1 = User ID
'    Return:
'    return 0 = Success, else is error code
'
'(10) Read User ID
'     Input:
'     function = 9
'     handle = dongle handle
'     Return:
'     lp1 = User ID
'     return 0 = Success, else is error code
'
'(11) Set Module [*]
'     Input:
'     function = 10
'     handle = dongle handle
'     p1 = module number
'     p2 = module content
'     p3 = set whether allow decrease (1 = allow, 0 = no allow)
'     Return:
'     return 0 = Success, else is error code
'
'(12) Check Module
'     Input:
'     function = 11
'     handle = dongle handle
'     p1 = module number
'     Return:
'     p2 = 1 means the module is valid, 0 means the module is invalid
'     p3 = 1 means the module can't decrease, 0 means the module can decrease
'     return 0 = Success, else is error code
'
'(13) Write Arithmetic [*]
'     Input:
'     function = 12
'     handle = dongle handle
'     p1 = pos
'     buffer = arithmetic instruction string
'     Return:
'     return 0 = Success, else is error code
'
'(14) Calculate 1 (Hide Unit Init Content = HID high 16bit, HID low 16bit, module content, random number)
'     Input:
'     function = 13
'     handle = dongle handle
'     lp1 = calculate begin pos
'     lp2 = module number
'     p1 = input value 1
'     p2 = input value 2
'     p3 = input value 3
'     p4 = input value 4
'     Return:
'     p1 = return code 1
'     p2 = return code 2
'     p3 = return code 3
'     p4 = return code 4
'     return 0 = Success, else is error code
'
'(15) Calculate 2 (Hide Unit Init Content = seed return code 1, seed return code 2, seed return code 3, seed return code 4)
'     Input:
'     function = 14
'     handle = dongle handle
'     lp1 = calculate begin pos
'     lp2 = seed code
'     p1 = input value 1
'     p2 = input value 2
'     p3 = input value 3
'     p4 = input value 4
'     Return:
'     p1 = return code 1
'     p2 = return code 2
'     p3 = return code 3
'     p4 = return code 4
'     return 0 = Success, else is error code
'
'(16) Calculate 3 (Hide Unit Init Content = module content, module+1 content, module+2 content, module+3 content)
'     Input:
'     function = 15
'     handle = dongle handle
'     lp1 = calculate begin pos
'     lp2 = module begin pos
'     p1 = input value 1
'     p2 = input value 2
'     p3 = input value 3
'     p4 = input value 4
'     Return:
'     p1 = return code 1
'     p2 = return code 2
'     p3 = return code 3
'     p4 = return code 4
'     return 0 = Success, else is error code
'
'(17) Decrease Module Unit
'     Input:
'     function = 16
'     handle = dongle handle
'     p1 = module number
'     Return:
'     return 0 = Success, else is error code

Declare Function Rockey Lib "RYDLL32.DLL" (ByVal fcode As Integer, ByRef handle As Integer, ByRef lp1 As Long, ByRef lp2 As Long, ByRef p1 As Integer, ByRef p2 As Integer, ByRef p3 As Integer, ByRef p4 As Integer, ByVal buffer As Any) As Integer

'Function Code
Global Const RY_FIND = 1                        'Find Dongle
Global Const RY_FIND_NEXT = 2                   'Find Next Dongle
Global Const RY_OPEN = 3                        'Open Dongle
Global Const RY_CLOSE = 4                       'Close Dongle
Global Const RY_READ = 5                        'Read Dongle
Global Const RY_WRITE = 6                       'Write Dongle
Global Const RY_RANDOM = 7                      'Generate Random Number
Global Const RY_SEED = 8                        'Generate Seed Code
Global Const RY_WRITE_USERID = 9                'Write User ID
Global Const RY_READ_USERID = 10                'Read User ID
Global Const RY_SET_MOUDLE = 11                 'Set Module
Global Const RY_CHECK_MOUDLE = 12               'Check Module
Global Const RY_WRITE_ARITHMETIC = 13           'Write Arithmetic
Global Const RY_CALCULATE1 = 14                 'Calculate 1
Global Const RY_CALCULATE2 = 15                 'Calculate 2
Global Const RY_CALCULATE3 = 16                 'Calculate 3
Global Const RY_DECREASE = 17                   'Decrease Module Unit

'Error Code
Global Const ERR_SUCCESS = 0                    'Success
Global Const ERR_NO_PARALLEL_PORT = 1           'No parallel port on the computer
Global Const ERR_NO_DRIVER = 2                  'No install drivers
Global Const ERR_NO_ROCKEY = 3                  'No rockey dongle
Global Const ERR_INVALID_PASSWORD = 4           'Found rockey dongle, but base password is wrong
Global Const ERR_INVALID_PASSWORD_OR_ID = 5     'Wrong password or rockey HID
Global Const ERR_SETID = 6                      'Set rockey HID wrong
Global Const ERR_INVALID_ADDR_OR_SIZE = 7       'Read/Write address is wrong
Global Const ERR_UNKNOWN_COMMAND = 8            'No such command
Global Const ERR_NOTBELEVEL3 = 9                'Inside error
Global Const ERR_READ = 10                      'Read error
Global Const ERR_WRITE = 11                     'Write error
Global Const ERR_RANDOM = 12                    'Random error
Global Const ERR_SEED = 13                      'Seed Code error
Global Const ERR_CALCULATE = 14                 'Calculate error
Global Const ERR_NO_OPEN = 15                   'No open dongle before operate dongle
Global Const ERR_OPEN_OVERFLOW = 16             'Too more open dongle(>16)
Global Const ERR_NOMORE = 17                    'No more dongle
Global Const ERR_NEED_FIND = 18                 'No Find before FindNext
Global Const ERR_DECREASE = 19                  'Decrease error

Global Const ERR_AR_BADCOMMAND = 20             'Arithmetic instruction error
Global Const ERR_AR_UNKNOWN_OPCODE = 21         'Arithmetic operator error
Global Const ERR_AR_WRONGBEGIN = 22             'Const number can't use on first arithmetic instruction
Global Const ERR_AR_WRONG_END = 23              'Const number can't use on last arithmetic instruction
Global Const ERR_AR_VALUEOVERFLOW = 24          'Const number > 63
Global Const ERR_UNKNOWN = &HFFFF               'Unknown error

Global Const ERR_RECEIVE_NULL = &H100           'Receive null
Global Const ERR_PRNPORT_BUSY = &H101           'Parallel busy
Global Const ERR_UNKNOWN_SYSTEM = &H102         'Unknown operate system

Public Function Cuint(ByVal v As Integer) As Long
Dim r As Long

If (v > 0) Then
r = v
Else
r = 65536 + v
End If
Cuint = r
End Function
