Unknown Reader in SCardGetStatusChange Winscard

-1

I am new to.net and I would like to identify if card is detected or removed using winscard's SCardGetStatusChange. But it is returning retcode = -2146435063 which when converted to Hex would give me 0x80100009 = SCARD_E_UNKNOWN_READER. I just based my code from pinvoke.net

Here's sample of my code

Public Structure SCARDREADER_STATE
    Public szReader As String
    Public pvUserData As IntPtr
    Public dwCurrentState As Integer
    Public dwEventState As Integer
    Public cbAtr As Integer
    Public rgbAtr() As Byte
End Structure

<DllImport(winscarddll, CharSet:=CharSet.Ansi, EntryPoint:="SCardGetStatusChangeA")>
Public Shared Function SCardGetStatusChange(ByVal hContext As IntPtr, ByVal dwTime As Integer, ByRef rgReaderState As SCARDREADER_STATE(), ByVal cReaders As Integer) As Integer
End Function

And in my form.

Public Function gAtr(ByVal hContext As IntPtr, ByVal reader As String) As String
    Dim ret As Integer
    Dim rs(0) As WinscardFun.SCARDREADER_STATE
    Console.Write(reader)
    'ReDim rs(0).rgbAtr(0 To 35)
    'rs(0).dwEventState = &H0
    'rs(0).rgbAtr = Nothing
    'rs(0).cbAtr = 0
    rs(0).szReader = reader ' "\\\\?PnP?\\Notification"
    rs(0).dwCurrentState = SCARD_STATE_UNAWARE

    ret = WinscardFun.SCardGetStatusChange(hContext, 100000000, rs, 1)
    Console.Write("Ret=" + ret.ToString + vbCrLf)
    Console.Write("Reader:" + rs(0).szReader + vbCrLf)
    If ret = 0 And rs(0).cbAtr > 0 And rs(0).rgbAtr IsNot Nothing Then
        test = "Success"
    Else
        test = "Fail"
    End If

    Return test
End Function

The output is

Ret=-2146435063 Reader:ACS ACR1222 3S PICC Reader PICC 0 Fail

vb.net
smartcard
contactless-smartcard
winscard
asked on Stack Overflow Sep 15, 2017 by Anonymouse24

1 Answer

0

For future researcher/developer.

In SCardGetStatusChange, I just changed the SCardReaderState() to SCardReader

<DllImport(winscarddll, EntryPoint:="SCardGetStatusChangeA", CharSet:=CharSet.Ansi)>
Public Shared Function SCardGetStatusChange(ByVal hContext As IntPtr, ByVal dwTime As Integer, ByRef rgReaderState As SCARDREADER_STATE, ByVal cReaders As Integer) As Integer
End Function

Then added the <MarshalAs(UnmanagedType.ByValArray, SizeConst:=36)> in the initialization of rgbAtr.

Hope this will helps.

answered on Stack Overflow Sep 15, 2017 by Anonymouse24

User contributions licensed under CC BY-SA 3.0