Private Sub Read(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ReaderCount As Integer
Dim ReaderCount As Integer
Dim ctr As Integer
Dim sReaderList As String
Dim ctr As Integer
Dim sReaderList As String
For ctr = 0 To 255
sReaderList = sReaderList + vbNullChar
Next
ReaderCount = 255
' 1. Establish context and obtain hContext handle
retCode = ModWinsCard.SCardEstablishContext(ModWinsCard.SCARD_SCOPE_USER, 0, 0, hContext)
' 2. List PC/SC card readers installed in the system
retCode = ModWinsCard.SCardListReaders(hContext, "", sReaderList, ReaderCount)
' Load Available Readers
Call LoadListToControl(cbReader, sReaderList)
cbReader.SelectedIndex = 0
If connActive Then
retCode = ModWinsCard.SCardDisconnect(hCard, ModWinsCard.SCARD_UNPOWER_CARD)
End If
' Shared Connection
retCode = ModWinsCard.SCardConnect(hContext, cbReader.SelectedItem.ToString(), ModWinsCard.SCARD_SHARE_SHARED, ModWinsCard.SCARD_PROTOCOL_T0 Or ModWinsCard.SCARD_PROTOCOL_T1, hCard, Protocol)
connActive = True
Connect()
End Sub
Public Sub Connect()
Dim tmpStr As String
Dim indx As Integer
validATS = False
Call ClearBuffers()
SendBuff(0) = &HFF ' CLA
SendBuff(1) = &HCA ' INS
SendBuff(3) = &H0 ' P2
SendBuff(4) = &H0 ' Le : Full Length
SendLen = SendBuff(4) + 5
RecvLen = &HFF
retCode = SendAPDUandDisplay(3)
If retCode <> ModWinsCard.SCARD_S_SUCCESS Then
Exit Sub
End If
For indx = 0 To (RecvLen - 3)
tmpStr = tmpStr + Microsoft.VisualBasic.Right("00" & Hex(RecvBuff(indx)), 2) + " "
Next indx
cardUID = tmpStr
Attendace_Menu.Show()
Me.Hide()
End Sub
I need help with my code. I want it to read the UID of the smart card not just when loading the form but also when I remove the card and insert it again. It only reads the card when I load the form. I have no idea what to do since I'm new to programming and this is my project for school.
You can use WinSCard library's SCardGetStatusChange function to observe changes via background worker thread.