Module Module1

    


    'NetROCKEY4 Commands.
    Enum Ry4CMD As Short
        RY_FIND = 1             'Find Rockey4.
        RY_FIND_NEXT            'Find next Rockey4.
        RY_OPEN                 'Open 
        RY_CLOSE                'Close
        RY_READ                 'Read UDZ
        RY_WRITE                'Write UDZ
        RY_RANDOM               'Random
        RY_SEED                 'Seed code		
        RY_READ_USERID = 10     'Read UID		
        RY_CHECK_MODULE = 12    'Check module status		
        RY_CALCULATE1 = 14      'Calculate 1
        RY_CALCULATE2           'Calculate 2
        RY_CALCULATE3           'Calculate 3
        RY_DECREASE             'Decrease
    End Enum

    'ROCKEY4 type definations
    Enum RY4TYPE As Integer
        TYPE_ROCKEY4 = 1        'Normal Ry4.
        TYPE_ROCKEY4Plus        'Ry4Plus.
        TYPE_ROCKEYUSB          'Ry4 USB.
        TYPE_ROCKEYUSBPlus      'Ry4 USB Plus
        TYPE_ROCKEYNET          'NetRy4
        TYPE_ROCKEYUSBNET       'NetRy4 USB.
    End Enum


    'NetROCKEY4 Result definations.
    Public Enum Ry4Result As Integer
        ERR_SUCCESS = &H0                       'No error.
        ERR_NO_PARALLEL_PORT = &H80300001       '(0x80300001)No parallel port on the computer 
        ERR_NO_DRIVER                           '(0x80300002)No driver installed 
        ERR_NO_ROCKEY                           '(0x80300003)No ROCKEY4 dongle 
        ERR_INVALID_PASSWORD                    '(0x80300004)ROCKEY4 dongle found  but base password is incorrect
        ERR_INVALID_PASSWORD_OR_ID              '(0x80300005)Wrong password or ROCKEY4 HID
        ERR_SETID                               '(0x80300006)Set ROCKEY4 HID wrong
        ERR_INVALID_ADDR_OR_SIZE                '(0x80300007)Read/Write address is wrong
        ERR_UNKNOWN_COMMAND                     '(0x80300008)No such command
        ERR_NOTBELEVEL3                         '(0x80300009)Inside error
        ERR_READ                                '(0x8030000A)Read error
        ERR_WRITE                               '(0x8030000B)Write error
        ERR_RANDOM                              '(0x8030000C)Random error
        ERR_SEED                                '(0x8030000D)Seed Code error
        ERR_CALCULATE                           '(0x8030000E)Calculate error
        ERR_NO_OPEN                             '(0x8030000F)Ry_Open must precede this operation
        ERR_OPEN_OVERFLOW                       '(0x80300010)Too many open dongles (>16)
        ERR_NOMORE                              '(0x80300011)No more dongle
        ERR_NEED_FIND                           '(0x80300012)No Find before FindNext
        ERR_DECREASE                            '(0x80300013)Decrease error
        ERR_AR_BADCOMMAND                       '(0x80300014)Arithmetic instruction error
        ERR_AR_UNKNOWN_OPCODE                   '(0x80300015)Arithmetic operator error
        ERR_AR_WRONGBEGIN                       '(0x80300016)A constant. cannot be in the first instruction
        ERR_AR_WRONG_END                        '(0x80300017)A constant. cannot be in the last instruction 
        ERR_AR_VALUEOVERFLOW                    '(0x80300018)Const number > 63

        ERR_UNKNOWN = &H8030FFFF                '(0x8030FFFF)Unknown error 

        ERR_RECEIVE_NULL = &H80300100           '(0x80300100)Parallel port can not recieve data.
        ERR_PRNPORT_BUSY = &H80300101           '(0x80300101)Parallel port is busy.



        'Error code for Net login

        ERR_NET_LOGINAGAIN = &H803003E9         'A module can only be opened once by the same process.  
        ERR_NET_NETERROR = &H803003EA           'Network error.
        ERR_NET_LOGIN = &H803003EB              'Too many users are logged on.
        ERR_NET_INVALIDHANDLE = &H803003EC      'Invalid handle  this handle might have been closed.
        ERR_NET_BADHARDWARE = &H803003ED        'Defective hardware 
        ERR_NET_REFUSE = &H803003EE             'Client dll modified, service refused request.
        ERR_NET_BADSERVER = &H803003EF          'Nrsvr.exe modified, service is invalid.



        'Error code for network

        ERR_INIT_SOCK = &H803007D1              'Error when initializing.
        ERR_NOSUCHPROTO = &H803007D2            'No such protocol.
        ERR_UDPSOCKCREATE = &H803007D3          'UDP error when creating socket.
        ERR_UDPSETBROADCAST = &H803007D4        'UDP error when setting broadcast.
        ERR_UDPBINDFAILED = &H803007D5          'UDP error when binding.
        ERR_SVRCALLBACKNULL = &H803007D6        'Server call back null.
        ERR_TCPSOCKCREATE = &H803007D7          'TCP error when creating socket.
        ERR_TCPBINDFAILED = &H803007D8          'TCP error when binding.
        ERR_TCPLISTENFAILED = &H803007D9        'TCP error when listening.
        ERR_NOSUCHSEARCH = &H803007DA           'No such search mode.
        ERR_UDPSEND = &H803007DC                'UDP error when sending.
        ERR_UDPTIMEOUT = &H803007DD             'UDP timeout error when waiting.
        ERR_UDPRECV = &H803007DE                'UDP error when receiving.
        ERR_TCPCONNECT = &H803007DF             'TCP error when connecting to server.
        ERR_TCPSENDTIMEOUT = &H803007E0         'TCP time out error when sending.
        ERR_TCPSEND = &H803007E1                'TCP error when sending.
        ERR_TCPRECVTIMEOUT = &H803007E2         'TCP time out error when receiving.
        ERR_TCPRECV = &H803007E3                'TCP error when receiving.
        ERR_IPXSOCKCREATE = &H803007E4          'IPX error when creating socket.
        ERR_IPXSETBROADCAST = &H803007E5        'IPX error when setting broadcast.
        ERR_IPXSEND = &H803007E6                'IPX error when sending data. 
        ERR_IPXRECV = &H803007E7                'IPX error when receiving data.
        ERR_IPXBIND = &H803007E8                'IPX error when binding.
        ERR_NBSRESET = &H803007E9               'NetBIOS error when initializing.
        ERR_NBSADDNAME = &H803007EA             'NetBIOS error when adding name.
        ERR_NBSSEND = &H803007EB                'NetBIOS error when sending data.
        ERR_NBSRECV = &H803007EC                'NetBIOS error when receiving data.

    End Enum

    Sub Main()


        'NetROCKEY4 Parameters
        Dim FuncID As UInt16            'Function ID
        Dim handle As UInt16            'Handle
        Dim Lp1, Lp2 As UInt32          'Long parameters
        Dim p1, p2, p3, p4 As UInt16    'Short parameters
        Dim Buf As Object               'Buffer



        'Create NetRockey class.
        Dim Class1 As New NetROCKEY4.CNetRockey4Class()


        'record found Rockey4(s).
        Dim iMaxRockey As Integer = 0
        Dim uiarrRy4ID(32) As UInt32
        Dim iCurrID, iModule As UInt32
        Dim csSvrName As String


        Try
            'demo0: Find rockey. id=0,need setting p1,p2 to password.
            FuncID = Convert.ToUInt16(Ry4CMD.RY_FIND)
            Buf = 0

            'Set password.
            p1 = Convert.ToUInt16(&HC44C)
            p2 = Convert.ToUInt16(&HC8F8)

            Console.WriteLine("Finding NetROCKEY4s...")
            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

            'Print result to screen. Lp1 is ID, buf is Server name.
            csSvrName = Buf
            Console.WriteLine("({0}) {1:X} @ {2}", iMaxRockey, Lp1, csSvrName)

            'record it.
            uiarrRy4ID(iMaxRockey) = Lp1
            iMaxRockey = iMaxRockey + 1

            While True

                'demo1: Find Next NetRockey.
                FuncID = Convert.ToUInt16(Ry4CMD.RY_FIND_NEXT)
                Buf = 0

                'Set password.
                p1 = Convert.ToUInt16(&HC44C)
                p2 = Convert.ToUInt16(&HC8F8)

                Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

                'Print result to screen. Lp1 is ID, buf is Server name.
                csSvrName = Buf
                Console.WriteLine("({0}) {1:X} @ {2}", iMaxRockey, Lp1, csSvrName)

                'record it.
                uiarrRy4ID(iMaxRockey) = Lp1
                iMaxRockey = iMaxRockey + 1


            End While


        Catch e As System.Runtime.InteropServices.COMException

            Dim hRet As Ry4Result = e.ErrorCode

            If hRet = Ry4Result.ERR_NO_ROCKEY Or hRet = Ry4Result.ERR_NOMORE Then
                'No rockey found or no more rockey found, Find OK, print result
                Console.WriteLine("Found {0} NetROCKEY4(s)", iMaxRockey)
            Else
                'other error code, print it in hex format. 
                Console.WriteLine("Failed, codr {0:X}, {1}", hRet, hRet.ToString())
            End If

        End Try


        'select  ID and Module to open.

        Console.WriteLine("Please input the index to open.(>=0,<{0})", iMaxRockey)
        iCurrID = uiarrRy4ID(Convert.ToInt32(Console.ReadLine()))

        Console.WriteLine("Please input the Module to login.")
        iModule = Convert.ToUInt32(Console.ReadLine())


        Try

            'Demo3: Open. LP1 is Ry4ID, LP2 is Module number.
            FuncID = Convert.ToUInt16(Ry4CMD.RY_OPEN)
            Lp1 = iCurrID
            Lp2 = iModule
            p1 = Convert.ToUInt16(&HC44C)
            p2 = Convert.ToUInt16(&HC8F8)

            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

            'opened succeeded. handle ID in handle, type information in lp2.
            Dim rType As RY4TYPE = Convert.ToInt32(Lp2)
            Console.WriteLine("Opened,handle is {0:X4},type is {1}", handle, rType.ToString)

            'you can see it in NrMon.exe now :)
            Console.WriteLine("Press any key to continue test...")
            Console.Read()

            'Demo4: Write 24 bytes to UDZ. p1=index of first address, p2=length,obBuffer=data,in byte array.
            'Caution: Normal Ry4(include Ry4USB) has 24 bytes UDZ, Ry4Net,Ry4Plus has 80 bytes UDZ.

            Dim byWriteData(24) As Byte
            Dim i As Byte

            For i = 0 To 23
                byWriteData(i) = i
            Next

            Console.Write("Writting 24 bytes to user data zone...")

            FuncID = Convert.ToUInt16(Ry4CMD.RY_WRITE)
            p1 = Convert.ToUInt16(0)
            p2 = Convert.ToUInt16(24)       '24 bytes
            Buf = byWriteData               'Set buf to byte array. you can also set it to a string.

            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)
            Console.WriteLine("[OK]")


            'Demo5: Read UDZ Zone, 24 bytes.
            Console.WriteLine("Reading 24 bytes from user data zone...")

            FuncID = Convert.ToUInt16(Ry4CMD.RY_READ)
            p1 = Convert.ToUInt16(0)
            p2 = Convert.ToUInt16(24)       '24 bytes
            Buf = 0                         'no input data

            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

            Dim byReadData() As Byte = Buf

            For i = 0 To 23
                Console.Write("{0:X2} ", byReadData(i))
            Next

            Console.WriteLine("[OK]")

            'Demo6: Generate 4 random number.

            Dim usRandom(4) As UInt16

            Console.WriteLine("Generating 8 bytes random...")

            For i = 0 To 3

                FuncID = Convert.ToUInt16(Ry4CMD.RY_RANDOM)
                Buf = 0                         'no input data

                Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

                usRandom(i) = p1        'Random in p1.

            Next

            ' Print result to screen
            For i = 0 To 3
                Console.Write("{0:X4} ", usRandom(i))
            Next

            Console.WriteLine("[OK]")

            'Demo7: Generate seed code. lp2 is seed.
            Console.WriteLine("Generating seed code, seed=0x193F4701...")

            FuncID = Convert.ToUInt16(Ry4CMD.RY_SEED)
            Lp2 = Convert.ToUInt32(&H193F4701)              'a 32 bit seed
            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

            Console.WriteLine("[OK], seed code is {0:X4} {1:X2} {2:X4} {3:X4}", p1, p2, p3, p4)

            'Demo 8 is not available for NetROCKEY4.

            'Demo9: Read UID.
            Console.WriteLine("Reading user ID...")

            FuncID = Convert.ToUInt16(Ry4CMD.RY_READ_USERID)
            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

            ' UID in lp1
            Console.WriteLine("[OK],User ID is {0:X8}", Lp1)


            'Demo 10 is not available for NetROCKEY4.

            'Demo11:Check module. p1=module index
            Console.WriteLine("Checking module 8...")

            FuncID = Convert.ToUInt16(Ry4CMD.RY_CHECK_MODULE)
            p1 = Convert.ToUInt16(8)
            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

            Console.WriteLine("[OK], bValidate={0}, bDecreasable={1}", p2, p3)

            'Demo12: Decrease 3 times. p1=module index.
            For i = 1 To 3

                Console.Write("Module 8,Decrease {0}... ", i)

                FuncID = Convert.ToUInt16(Ry4CMD.RY_DECREASE)
                p1 = Convert.ToUInt16(8)
                Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

                'Check module status after every decrease
                FuncID = Convert.ToUInt16(Ry4CMD.RY_CHECK_MODULE)
                p1 = Convert.ToUInt16(8)
                Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

                Console.WriteLine("[OK], bValidate={0}, bDecreasable={1}", p2, p3)

            Next

            'Demo 13 is not available for NetROCKEY4.


            'Demo14,Calculate 1
            Console.Write("Calculate 1 ...")

            FuncID = Convert.ToUInt16(Ry4CMD.RY_CALCULATE1)

            'Set parameters
            Lp1 = Convert.ToUInt32(0)
            Lp2 = Convert.ToUInt32(8)

            p1 = Convert.ToUInt16(1)
            p2 = Convert.ToUInt16(2)
            p3 = Convert.ToUInt16(3)
            p4 = Convert.ToUInt16(4)

            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

            'print result to screen.
            Console.WriteLine("[OK],result: A={0:X4},B={1:X4},C={2:X4},D={3:X4}", p1, p2, p3, p4)


            'Demo15,Calculate 2
            Console.Write("Calculate 2 ...")

            FuncID = Convert.ToUInt16(Ry4CMD.RY_CALCULATE2)

            'Set parameters
            Lp1 = Convert.ToUInt32(0)
            Lp2 = Convert.ToUInt32(&H19303A09)

            p1 = Convert.ToUInt16(1)
            p2 = Convert.ToUInt16(2)
            p3 = Convert.ToUInt16(3)
            p4 = Convert.ToUInt16(4)

            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

            'print result to screen.
            Console.WriteLine("[OK],result: A={0:X4},B={1:X4},C={2:X4},D={3:X4}", p1, p2, p3, p4)

            'Demo16,Calculate 3
            Console.Write("Calculate 3 ...")

            FuncID = Convert.ToUInt16(Ry4CMD.RY_CALCULATE3)

            'Set parameters
            Lp1 = Convert.ToUInt32(0)
            Lp2 = Convert.ToUInt32(8)

            p1 = Convert.ToUInt16(1)
            p2 = Convert.ToUInt16(2)
            p3 = Convert.ToUInt16(3)
            p4 = Convert.ToUInt16(4)

            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

            'print result to screen.
            Console.WriteLine("[OK],result: A={0:X4},B={1:X4},C={2:X4},D={3:X4}", p1, p2, p3, p4)




            'Demo17,Close.           
            Console.Write("Closing handle {0:X}...", handle)

            FuncID = Convert.ToUInt16(Ry4CMD.RY_CLOSE)
            Buf = 0
            Class1.DoCmd(FuncID, handle, Lp1, Lp2, p1, p2, p3, p4, Buf)

            Console.WriteLine("[OK]")



        Catch e As System.Runtime.InteropServices.COMException

            Dim hRet As Ry4Result = e.ErrorCode

            Console.WriteLine("Failed, codr {0:X}, {1}", hRet, hRet.ToString())



        End Try







    End Sub

End Module
