Sample code for 30+ languages & platforms
PureBasic

PC/SC Wait for Smart Card Status Change (Inserted, Removed from Reader, etc.)

See more SCard Examples

Demonstrates how to synchronously wait for a status change, such as for a smart card to be inserted into a reader, or removed from a reader.

Note: This functionality was introduced in Chilkat v9.5.0.87.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringTable.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkSCard.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    scard.i = CkSCard::ckCreate()
    If scard.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; First establish a context to the PC/SC Resource Manager
    success = CkSCard::ckEstablishContext(scard,"user")
    If success = 0
        Debug CkSCard::ckLastErrorText(scard)
        CkSCard::ckDispose(scard)
        ProcedureReturn
    EndIf

    ; First we'll examine the state of all connected readers to see which have smartcards already inserted..
    ; Get JSON containing information about the smartcards currently inserted into readers.
    ; This also includes information about USB security tokens.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSCard::ckFindSmartcards(scard,json)
    If success = 0
        Debug CkSCard::ckLastErrorText(scard)
        CkSCard::ckDispose(scard)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ; When writing this example, I have 3 smart card readers plugged into my system.
    ; One of them has a smart card inserted.
    ; Here's the JSON returned by FindSmartcards...

    ; {
    ;   "reader": [
    ;     {
    ;       "name": "Alcor Micro USB Smart Card Reader 0",
    ;       "state": "empty"
    ;     },
    ;     {
    ;       "name": "Generic Smart Card Reader Interface 0",
    ;       "state": "present",
    ;       "vendorName": "Generic",
    ;       "serialNumber": "3230303730383138303030303030303030",
    ;       "systemName": "Generic Smart Card Reader Interface 0",
    ;       "card": {
    ;         "atr": "3BFC180000813180459067464A00641606F2727E00E0",
    ;         "windows": {
    ;           "miniDriver": "tagliov70px.dll",
    ;           "cryptoProvider": "Microsoft Base Smart Card Crypto Provider",
    ;           "keyStorageProvider": "Microsoft Smart Card Key Storage Provider"
    ;         }
    ;       }
    ;     },
    ;     {
    ;       "name": "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0",
    ;       "state": "empty"
    ;     }
    ;   ]
    ; }

    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)
    Debug " "

    ; We can iterate over the JSON to find the readers with a smart card already inserted..
    name.s
    sbState.i = CkStringBuilder::ckCreate()
    If sbState.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(json,"reader")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        name = CkJsonObject::ckStringOf(json,"reader[i].name")

        CkStringBuilder::ckClear(sbState)
        CkJsonObject::ckStringOfSb(json,"reader[i].state",sbState)

        If CkStringBuilder::ckContains(sbState,"present",1) = 1
            Debug name + " has a smart card inserted."
        Else
            Debug name + " does not have a smart card inserted."
        EndIf

        i = i + 1
    Wend
    Debug " "

    ; Now let's begin the code to wait for a change (where a smart card gets inserted or removed)

    ; Get the list of all readers.
    stReaders.i = CkStringTable::ckCreate()
    If stReaders.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSCard::ckListReaders(scard,stReaders)
    If success = 0
        Debug CkSCard::ckLastErrorText(scard)
        CkSCard::ckDispose(scard)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbState)
        CkStringTable::ckDispose(stReaders)
        ProcedureReturn
    EndIf

    ; Show the reader names..
    numReaders.i = CkStringTable::ckCount(stReaders)
    i = 0
    While i < numReaders
        Debug Str(i) + ": " + CkStringTable::ckStringAt(stReaders,i)
        i = i + 1
    Wend

    ; Sample output from the above loop.
    ; 0: Alcor Micro USB Smart Card Reader 0
    ; 1: Generic Smart Card Reader Interface 0
    ; 2: SCM Microsystems Inc. SCR33x USB Smart Card Reader 0
    ; 

    ; Synchronously wait 30 seconds for a card to be inserted or removed from any of the above readers.
    json2.i = CkJsonObject::ckCreate()
    If json2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSCard::ckGetStatusChange(scard,30000,stReaders,json2)
    If success = 0
        Debug CkSCard::ckLastErrorText(scard)
        CkSCard::ckDispose(scard)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbState)
        CkStringTable::ckDispose(stReaders)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

    Debug " "

    ; Let's see what happened...
    CkJsonObject::setCkEmitCompact(json2, 0)
    Debug CkJsonObject::ckEmit(json2)
    Debug " "

    ; This is what json2 contains.
    ; A card was inserted into the reader named "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0"
    ; The "numChanged" indicates that one reader's status changed.  
    ; The "changed":true indicates the reader that changed.   The state is now "present".

    ; {
    ;   "numChanged": 1,
    ;   "reader": [
    ;     {
    ;       "name": "Alcor Micro USB Smart Card Reader 0",
    ;       "changed": false,
    ;       "state": "empty"
    ;     },
    ;     {
    ;       "name": "Generic Smart Card Reader Interface 0",
    ;       "changed": false,
    ;       "state": "present",
    ;       "atr": "3BFC180000813180459067464A00641606F2727E00E0"
    ;     },
    ;     {
    ;       "name": "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0",
    ;       "changed": true,
    ;       "state": "present",
    ;       "atr": "3BDF96FF8131FE455A018048494443313158587300011B09"
    ;     }
    ;   ]
    ; }

    ; Find the reader that changed...
    changed.i
    state.s
    atr.s

    numChanged.i = CkJsonObject::ckIntOf(json2,"numChanged")
    Debug "number of readers with a changed state: " + Str(numChanged)

    i = 0
    count_i = CkJsonObject::ckSizeOfArray(json2,"reader")
    While i < count_i
        CkJsonObject::setCkI(json2, i)
        changed = CkJsonObject::ckBoolOf(json2,"reader[i].changed")
        If changed = 1
            name = CkJsonObject::ckStringOf(json2,"reader[i].name")
            state = CkJsonObject::ckStringOf(json2,"reader[i].state")

            Debug "Changed: "
            Debug "    reader name: " + name
            Debug "    new state: " + state

            ; If a card is now in this reader, we should have the ATR of the card..
            If CkJsonObject::ckHasMember(json2,"reader[i].atr") = 1
                atr = CkJsonObject::ckStringOf(json2,"reader[i].atr")
                Debug "    ATR of card inserted into this reader: " + atr
            EndIf

        EndIf

        i = i + 1
    Wend

    ; The output from the above loop:

    ; number of readers with a changed state: 1
    ; Changed: 
    ;     reader name: SCM Microsystems Inc. SCR33x USB Smart Card Reader 0
    ;     new state: present
    ;     ATR of card inserted into this reader: 3BDF96FF8131FE455A018048494443313158587300011B09

    ; Applications should always release the context when finished.
    success = CkSCard::ckReleaseContext(scard)
    If success = 0
        Debug CkSCard::ckLastErrorText(scard)
    EndIf



    CkSCard::ckDispose(scard)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbState)
    CkStringTable::ckDispose(stReaders)
    CkJsonObject::ckDispose(json2)


    ProcedureReturn
EndProcedure