Attribute VB_Name = "Module1"
' ---------------------------------------------------------
' (C) Copyright 2003 SenseLock Info & Secu Inc.
'
' Briefs£ºconstant definition, structure definition, all function definition provided by SenseLock EL API
'
' Authore£º chency
'
' Date£º03/2004
'
' -------------------------------------------------------

Public Const MAX_ATR_LEN = 56
Public Const MAX_ID_LEN = 8

Public Type SENSE4_CONTEXT
    dwIndex                        As Long                                 ' device index, starting from zero
    dwVersion                      As Long                                 ' version
    hLock                          As Long                                 ' device handle
    reserve(0 To 11)               As Byte                                 ' reserved
    bAtr(0 To MAX_ATR_LEN - 1)     As Byte                                 ' ATR information
    bID(0 To MAX_ID_LEN - 1)       As Byte                                 ' ID
    dwAtrLen                       As Long                                 ' ATR length
End Type


Public Const S4_RSA_MODULUS_LEN = 128
Public Const S4_RSA_PRIME_LEN = 64

' structure definition of RSA public file
Public Type S4_RSA_PUBLIC_KEY
  modulus(0 To S4_RSA_MODULUS_LEN - 1)      As Byte                         ' modulus
  exponent(0 To S4_RSA_MODULUS_LEN - 1)     As Byte                         ' public exponent
End Type


' structure definition of RSA private file
Public Type S4_RSA_PRIVATE_KEY
   modulus(0 To S4_RSA_MODULUS_LEN - 1)         As Byte                         ' modulus
   publicExponent(0 To S4_RSA_MODULUS_LEN - 1)  As Byte                         ' public exponent
   exponent(0 To S4_RSA_MODULUS_LEN - 1)        As Byte                         ' private exponent
   prime(0 To S4_RSA_MODULUS_LEN - 1)           As Byte                         ' prime factors
   primeExponent(0 To S4_RSA_MODULUS_LEN - 1)   As Byte                         ' exponents for CRT
   coefficient(0 To S4_RSA_PRIME_LEN - 1)       As Byte                         ' CRT coefficient
End Type



Public Type S4OPENINFO
    dwS4OpenInfoSize    As Long
    dwShareMode         As Long
End Type

Public Type S4CREATEDIRINFO
    dwS4CreateDirInfoSize   As Long
    szAtr(0 To 7)           As Byte
End Type


Public Const MAX_DEVICE_NUMBER = 16
Public Const ctxsize = 92


Public Const S4_EXCLUSIZE_MODE = 0
Public Const S4_SHARE_MODE = 1

Public Const S4_LED_UP = 4
Public Const S4_LED_DOWN = 8
Public Const S4_LED_WINK = 40

Public Const S4_GET_DEVICE_TYPE = 37
Public Const S4_GET_SERIAL_NUMBER = 38
Public Const S4_GET_VM_TYPE = 39
Public Const S4_GET_DEVICE_USABLE_SPACE = 41
Public Const S4_SET_DEVICE_ID = 42
Public Const S4_EF_INFO = 50


Public Const S4_SET_USB_MODE = 65


Public Const S4_SET_HID_MODE = 66



Public Const S4_LOCAL_DEVICE = 0
Public Const S4_MASTER_DEVICE = 1
Public Const S4_SLAVE_DEVICE = 2


Public Const S4_VM_51 = 0
Public Const S4_VM_251_BINARY = 1
Public Const S4_VM_251_SOURCE = 2



Public Const S4_USER_PIN = 161
Public Const S4_DEV_PIN = 162
Public Const S4_AUTHEN_PIN = 163

Public Const S4_RSA_PUBLIC_FILE = 6
Public Const S4_RSA_PRIVATE_FILE = 7
Public Const S4_EXE_FILE = 8
Public Const S4_DATA_FILE = 9
Public Const S4_XA_EXE_FILE = 11


Public Const S4_CREATE_NEW = 165
Public Const S4_UPDATE_FILE = 166
Public Const S4_KEY_GEN_RSA_FILE = 167
Public Const S4_SET_LICENCES = 168

Public Const S4_FILE_READ_WRITE = 0
Public Const S4_CREATE_NEW_FILE_EXECUTE_ONLY = 421
Public Const S4_CREATE_NEW_PEDDING_FILE = 8357

Public Const S4_CREATE_ROOT_DIR = 171
Public Const S4_CREATE_SUB_DIR = 172
Public Const S4_CREATE_MODULE = 173


Public Const S4_VM_EXE = 0
Public Const S4_XA_EXE = 1


'the return value
Public Const S4_SUCCESS = 0                                     ' success
Public Const S4_UNPOWERED = 1
Public Const S4_INVALID_PARAMETER = 2
Public Const S4_COMM_ERROR = 3
Public Const S4_PROTOCOL_ERROR = 4
Public Const S4_DEVICE_BUSY = 5
Public Const S4_KEY_REMOVED = 6
Public Const S4_INSUFFICIENT_BUFFER = 17
Public Const S4_NO_LIST = 18
Public Const S4_GENERAL_ERROR = 19
Public Const S4_UNSUPPORTED = 20
Public Const S4_DEVICE_TYPE_MISMATCH = 32
Public Const S4_FILE_SIZE_CROSS_7FFF = 33
Public Const S4_DIRECTORY_EXIST = 26881
Public Const S4_DEVICE_UNSUPPORTED = 27265
Public Const S4_FILE_NOT_FOUND = 27266
Public Const S4_INSUFFICIENT_SECU_STATE = 27010
Public Const S4_FILE_EXIST = 27264
Public Const S4_INSUFFICIENT_SPACE = 27268
Public Const S4_OFFSET_BEYOND = 27392
Public Const S4_PIN_BLOCK = 27011
Public Const S4_FILE_TYPE_MISMATCH = 27009
Public Const S4_CRYPTO_KEY_NOT_FOUND = 37891
Public Const S4_APPLICATION_TEMP_BLOCK = 27013
Public Const S4_APPLICATION_PERM_BLOCK = 37635
Public Const S4_DATA_BUFFER_LENGTH_ERROR = 26368
Public Const S4_CODE_RANGE = 65536
Public Const S4_CODE_RESERVED_INST = 131072
Public Const S4_CODE_RAM_RANGE = 262144
Public Const S4_CODE_BIT_RANGE = 524288
Public Const S4_CODE_SFR_RANGE = 1048576
Public Const S4_CODE_XRAM_RANGE = 2097152




Public Declare Function S4Enum Lib "sense4" (ByRef s4CtxList As SENSE4_CONTEXT, ByRef dwCtxListSize As Long) As Long
                                        
Public Declare Function S4Open Lib "sense4" (ByRef s4CtxList As SENSE4_CONTEXT) As Long
                                        
Public Declare Function S4Close Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT) As Long

Public Declare Function S4Control Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal ctlCode As Long, ByRef inBuff As Any, ByVal inBuffLen As Long, ByRef outBuff As Any, ByVal outBuffLen As Long, ByRef bytesReturned As Long) As Long

Public Declare Function S4CreateDir Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal szDirID As String, ByVal dwDirSize As Long, ByVal dwFlags As Long) As Long
                                            
Public Declare Function S4ChangeDir Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal lpszPath As String) As Long
                                            
Public Declare Function S4EraseDir Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal szDirID As Long) As Long

Public Declare Function S4VerifyPin Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT, ByRef lpPin As Byte, ByVal dwPinLen As Long, ByVal dwPinType As Long) As Long
                                            
Public Declare Function S4ChangePin Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT, ByRef lpOldPin As Byte, ByVal dwOldPinLen As Long, ByRef lpNewPin As Byte, ByVal dwNewPinLen As Long, ByVal dwPinType As Long) As Long

Public Declare Function S4ChangePin1 Lib "sense4" Alias "S4ChangePin" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal lpOldPin As Byte, ByVal dwOldPinLen As Long, ByRef lpNewPin As Byte, ByVal dwNewPinLen As Long, ByVal dwPinType As Long) As Long


Public Declare Function S4WriteFile Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal szFileID As String, ByVal dwOffset As Long, ByRef lpBuffer As Any, ByVal dwBufferSize As Long, ByVal dwFileSize As Long, ByRef lpBytesWritten As Long, ByVal dwFlags As Long, ByVal dwFileType As Long) As Long
                                            
Public Declare Function S4WriteFile1 Lib "sense4" Alias "S4WriteFile" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal szFileID As String, ByVal dwOffset As Long, ByVal lpBuffer As Long, ByVal dwBufferSize As Long, ByVal dwFileSize As Long, ByRef lpBytesWritten As Long, ByVal dwFlags As Long, ByVal dwFileType As Long) As Long
  
  
Public Declare Function S4WriteFile2 Lib "sense4" Alias "S4WriteFile" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal szFileID As Long, ByVal dwOffset As Long, ByVal lpBuffer As Long, ByVal dwBufferSize As Long, ByVal dwFileSize As Long, ByRef lpBytesWritten As Long, ByVal dwFlags As Long, ByVal dwFileType As Long) As Long
  
                                            
Public Declare Function S4Execute Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal szFileID As String, ByRef lpInBuffer As Any, ByVal dwInbufferSize As Long, ByRef lpOutBuffer As Any, ByVal dwOutBufferSize As Long, ByRef lpBytesReturned As Long) As Long

Public Declare Function S4ExecuteEx Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal szFileID As String, ByVal dwFlag As Long, ByRef lpInBuffer As Any, ByVal dwInbufferSize As Long, ByRef lpOutBuffer As Any, ByVal dwOutBufferSize As Long, ByRef lpBytesReturned As Long) As Long

Public Declare Function S4OpenEx Lib "sense4" (ByRef s4CtxList As SENSE4_CONTEXT, ByRef pS4OpenInfo As S4OPENINFO) As Long



Public Declare Function S4CreateDirEx Lib "sense4" (ByRef s4Ctx As SENSE4_CONTEXT, ByVal lpszDirID As String, ByVal dwDirSize As Long, ByVal dwFlags As Long, ByRef pCreateDirInfo As S4CREATEDIRINFO) As Long

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long


Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)




Public Declare Function GetCurrentDirectory Lib "kernel32" Alias "GetCurrentDirectoryA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

