Torna al Thread
Dim hContext As Long
Dim hCard As Long
Dim retval As Long
Dim readers As String * 256
Dim groups As String * 256
Dim activeprotocol As Long
Dim readerlen As Long
retval = SCardEstablishContext(SCARD_SCOPE_USER, 0, 0, hContext)
If (retval <> 0) Then MsgBox ("Errore: " & CStr(retval))
readerlen = 256
' FACCIO UNA LISTA DEI LETTORI DISPONIBILI
retval = SCardListReaders(hContext, groups, readers, readerlen)
If (retval <> 0) Then MsgBox ("Lettore non trovato!")
' ######################################################################
' APRO LA CONNESSIONE CON LA CARTA
retval = SCardConnect(hContext, GetReader(readers, 1), SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0_OR_T1, hCard, activeprotocol)
If (retval <> 0) Then
MsgBox ("Tessera non inserita. Errore: " & CStr(retval))
Exit Sub
End If
' ######################################################################
Dim apdu(261) As Byte
Dim recvbuf(258) As Byte
Dim recvlen As Integer
Dim iosendreq As SCARD_IO_REQUEST
Dim iorecvreq As SCARD_IO_REQUEST
' STRINGA APDU
apdu(0) = &H0 'CLA
apdu(1) = &HCA 'INS
apdu(2) = &H0 'P1
apdu(3) = &H81 'P2
apdu(4) = &H0 'LC
apdu(5) = &H0 'LE
recvlen = 257
iosendreq.dwprotocol = activeprotocol
iosendreq.dbPciLength = Len(iosendreq)
iorecvreq.dwprotocol = activeprotocol
iorecvreq.dbPciLength = Len(iosendreq)
retval = SCardTransmit(hCard, iosendreq, apdu(0), 6, iorecvreq, recvbuf(0), recvlen)
If (retval <> 0) Then MsgBox ("Errore nella trasmissione: " & CStr(retval))
'---------------------
' LIVELLO MF
'---------------------
apdu(0) = &H0 'CLA
apdu(1) = &HA4 'INS
apdu(2) = &H9 'P1
apdu(3) = &H0 'P2
apdu(4) = &H2 'LC
apdu(5) = &H3F
apdu(6) = &H0
recvlen = 257
iosendreq.dwprotocol = activeprotocol
iosendreq.dbPciLength = Len(iosendreq)
iorecvreq.dwprotocol = activeprotocol
iorecvreq.dbPciLength = Len(iosendreq)
retval = SCardTransmit(hCard, iosendreq, apdu(0), 7, iorecvreq, recvbuf(0), recvlen)
If (retval <> 0) Then
MsgBox ("Errore n." & CStr(retval))
End If
'---------------------
' LIVELLO EF 1100
'---------------------
apdu(0) = &H0 'CLA
apdu(1) = &HA4 'INS
apdu(2) = &H9 'P1
apdu(3) = &H0 'P2
apdu(4) = &H2 'LC
apdu(5) = &H11
apdu(6) = &H0
recvlen = 257
iosendreq.dwprotocol = activeprotocol
iosendreq.dbPciLength = Len(iosendreq)
iorecvreq.dwprotocol = activeprotocol
iorecvreq.dbPciLength = Len(iosendreq)
retval = SCardTransmit(hCard, iosendreq, apdu(0), 7, iorecvreq, recvbuf(0), recvlen)
If (retval <> 0) Then
MsgBox ("Errore n." & CStr(retval))
End If
'---------------------
' LIVELLO EF 1102 - DATI PERSONALI CONTENUTI NELLA CARTA
'---------------------
apdu(0) = &H0 'CLA
apdu(1) = &HA4 'INS
apdu(2) = &H9 'P1
apdu(3) = &H0 'P2
apdu(4) = &H2 'LC
apdu(5) = &H11
apdu(6) = &H2
recvlen = 257
iosendreq.dwprotocol = activeprotocol
iosendreq.dbPciLength = Len(iosendreq)
iorecvreq.dwprotocol = activeprotocol
iorecvreq.dbPciLength = Len(iosendreq)
retval = SCardTransmit(hCard, iosendreq, apdu(0), 7, iorecvreq, recvbuf(0), recvlen)
If (retval <> 0) Then
MsgBox ("Errore n." & CStr(retval))
End If
' LEGGO I BINARY PROVENIENTI DALLA CARTA
apdu(0) = &H0 'CLA
apdu(1) = &HB0 'INS
apdu(2) = &H0 'P1
apdu(3) = &H0 'P2
apdu(4) = &H96 'LC
recvlen = 257
iosendreq.dwprotocol = activeprotocol
iosendreq.dbPciLength = Len(iosendreq)
iorecvreq.dwprotocol = activeprotocol
iorecvreq.dbPciLength = Len(iosendreq)
retval = SCardTransmit(hCard, iosendreq, apdu(0), 5, iorecvreq, recvbuf(0), recvlen)
If (retval <> 0) Then
MsgBox ("Errore n." & CStr(retval))
End If
risposta = ""
For K = 0 To apdu(4) - 1: risposta = risposta + Chr(recvbuf(K)): Next K
' ESTRAGGO COGNOME, NOME, CODICE FISCALE
' IN BASE ALLA POSIZIONE NELLA STRINGA DI RISPOSTA DALLA CARTA
Dim PosIni
PosIni = 1
risposta = Trim(risposta)
risposta = Mid(risposta, 7)
LenEmettitore = HexToLong(Int(Mid(risposta, PosIni, 2)))
PosIni = PosIni + 2
Emettitore = Mid(risposta, PosIni, LenEmettitore)
PosIni = PosIni + LenEmettitore
LenRilascio = Int(Mid(risposta, PosIni, 2))
PosIni = PosIni + 2
Rilascio = Mid(risposta, PosIni, LenRilascio)
PosIni = PosIni + LenRilascio
LenScadenza = Int(Mid(risposta, PosIni, 2))
PosIni = PosIni + 2
Scadenza = Mid(risposta, PosIni, LenScadenza)
PosIni = PosIni + LenScadenza
LenCognome = Int(Mid(risposta, PosIni, 2))
PosIni = PosIni + 2
Cognome = Mid(risposta, PosIni, LenCognome)
txtDati.Text = Cognome
PosIni = PosIni + LenCognome
LenNome = Int(Mid(risposta, PosIni, 2))
PosIni = PosIni + 2
Nome = Mid(risposta, PosIni, LenNome)
txtDati.Text = txtDati.Text & " - " & Nome
PosIni = PosIni + LenNome
LenValore1 = Int(Mid(risposta, PosIni, 2))
PosIni = PosIni + 2
PosIni = PosIni + LenValore1
LenValore2 = Int(Mid(risposta, PosIni, 2))
PosIni = PosIni + 2
PosIni = PosIni + LenValore2
LenValore3 = Int(Mid(risposta, PosIni, 2))
PosIni = PosIni + 2
PosIni = PosIni + LenValore3
LenCodice = HexToLong(Int(Mid(risposta, PosIni, 2)))
PosIni = PosIni + 2
Codice = Mid(risposta, PosIni, LenCodice)
txtDati.Text = txtDati.Text & " - " & Codice
' ############################################################################
' CHIUDO LA CONNESSIONE CON LA CARTA
retval = SCardDisconnect(hCard, SCARD_LEAVE_CARD)
If (retval <> 0) Then MsgBox ("Errore: " & CStr(retval))
retval = SCardReleaseContext(hContext)
If (retval <> 0) Then MsgBox ("Errore: " & CStr(retval))
' ############################################################################