-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

  • are you sure you have driver for card reader install ? did you ever successful using your card reader via other app tools on your machine ? – WallSky Blue Sep 15 '17 at 03:32
  • Yes, in fact I was successfully use the scardconnect and scardreaderlist. Therefore I am sure that the reader is working properly. I just want to use the scardgetstatuschange to identify if card is detected or not. – Anonymouse24 Sep 15 '17 at 03:47
  • could you provide result from SCardListReaders – WallSky Blue Sep 15 '17 at 03:55
  • The reader name you used is not correct. Doubling the backslashes like that is something you'd do in a language like C or C#, not in VB.NET – Hans Passant Sep 15 '17 at 04:47
  • @WallSkyBlue I am using ACR1222L, the results are:ACS ACR1222 3S PICC Reader PICC 0 ACS ACR1222 3S PICC Reader SAM 0 ACS ACR1222 3S PICC Reader SAM 1 ACS ACR1222 3S PICC Reader SAM 2 – Anonymouse24 Sep 15 '17 at 05:19
  • @HansPassant The '\\\\?PnP?\\Notification' refers to the manual from [msdn](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379773(v=vs.85).aspx) but I didn't use it thats why I just add it to comment section. – Anonymouse24 Sep 15 '17 at 05:22
  • uh ..... then I think your reader name in `ByVal reader As String` should be "ACS ACR1222 3S PICC Reader" – WallSky Blue Sep 15 '17 at 07:36
  • @WallSkyBlue thanks but still not working. Still giving unknown card – Anonymouse24 Sep 15 '17 at 07:48
  • I finally solved by adding on my rgbAtr. Haha. phew – Anonymouse24 Sep 15 '17 at 08:57
  • You might want to use [pcsc-sharp](https://github.com/danm-de/pcsc-sharp) or have a look at their way doing it. – vlp Sep 20 '17 at 11:38

1 Answers1

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.