Sample code for 30+ languages & platforms
Unicode C

PC/SC Find Inserted Smart Cards

See more SCard Examples

Get detailed information about each reader including whether or not a smart card is currently inserted in the reader. (Also includes USB tokens.)

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

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSCardW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSCardW scard;
    HCkJsonObjectW json;
    const wchar_t *name;
    const wchar_t *state;
    const wchar_t *vendorName;
    const wchar_t *systemName;
    const wchar_t *cardAtr;
    const wchar_t *cardWindowsMiniDriver;
    const wchar_t *cardWindowsCryptoProvider;
    const wchar_t *cardWindowsKeyStorageProvider;
    const wchar_t *serialNumber;
    int i;
    int count_i;

    success = FALSE;

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

    scard = CkSCardW_Create();

    // First establish a context to the PC/SC Resource Manager
    success = CkSCardW_EstablishContext(scard,L"user");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSCardW_lastErrorText(scard));
        CkSCardW_Dispose(scard);
        return;
    }

    // Get JSON containing information about the smartcards currently inserted into readers.
    // This also includes information about USB security tokens.
    json = CkJsonObjectW_Create();
    success = CkSCardW_FindSmartcards(scard,json);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSCardW_lastErrorText(scard));
        CkSCardW_Dispose(scard);
        CkJsonObjectW_Dispose(json);
        return;
    }

    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    // Here is sample output:
    // See below for sample code to parse the JSON.

    // You can see which readers have a card inserted by the "state".
    // If the state contains "present", then a card is inserted into the reader.
    // (USB tokens will typically always have a state containing "present")
    // Also, the ATR of the card inserted into the reader is indicated by "atr".

    // {
    //   "reader": [
    //     {
    //       "name": "Alcor Micro USB Smart Card Reader 0",
    //       "state": "present,inuse",
    //       "vendorName": "Alcor Micro",
    //       "systemName": "Alcor Micro USB Smart Card Reader 0",
    //       "card": {
    //         "atr": "3B7F96000080318065B0855956FB120FFE829000",
    //         "windows": {
    //           "miniDriver": "AxaltoCM.dll",
    //           "cryptoProvider": "Microsoft Base Smart Card Crypto Provider",
    //           "keyStorageProvider": "Microsoft Smart Card Key Storage Provider"
    //         }
    //       }
    //     },
    //     {
    //       "name": "FS USB Token 0",
    //       "state": "present,inuse",
    //       "vendorName": "FS",
    //       "serialNumber": "3F",
    //       "systemName": "FS USB Token 0",
    //       "card": {
    //         "atr": "3B9F958131FE9F006646530534002571DF000000000012",
    //         "windows": {
    //           "miniDriver": "eps2003csp11",
    //           "cryptoProvider": "Microsoft Base Smart Card Crypto Provider",
    //           "keyStorageProvider": "Microsoft Smart Card Key Storage Provider"
    //         }
    //       }
    //     },
    //     {
    //       "name": "FT Java Token 0",
    //       "state": "present",
    //       "vendorName": "FT",
    //       "serialNumber": "3F",
    //       "systemName": "FT Java Token 0",
    //       "card": {
    //         "atr": "3BFC180000813180459067464A00642D70C172FEE0FE",
    //         "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": "present",
    //       "vendorName": "SCM Microsystems Inc.",
    //       "serialNumber": "333130303330",
    //       "systemName": "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0",
    //       "card": {
    //         "atr": "3BFC180000813180459067464A00641606F2727E00E0",
    //         "windows": {
    //           "miniDriver": "tagliov70px.dll",
    //           "cryptoProvider": "Microsoft Base Smart Card Crypto Provider",
    //           "keyStorageProvider": "Microsoft Smart Card Key Storage Provider"
    //         }
    //       }
    //     },
    //     {
    //       "name": "Yubico YubiKey OTP+FIDO+CCID 0",
    //       "state": "present",
    //       "vendorName": "Yubico",
    //       "serialNumber": "3F",
    //       "systemName": "Yubico YubiKey OTP+FIDO+CCID 0",
    //       "card": {
    //         "atr": "3BFD1300008131FE158073C021C057597562694B657940",
    //         "windows": {
    //           "miniDriver": "msclmd.dll",
    //           "cryptoProvider": "Microsoft Base Smart Card Crypto Provider",
    //           "keyStorageProvider": "Microsoft Smart Card Key Storage Provider"
    //         }
    //       }
    //     }
    //   ]
    // }

    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"reader");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        name = CkJsonObjectW_stringOf(json,L"reader[i].name");
        state = CkJsonObjectW_stringOf(json,L"reader[i].state");
        vendorName = CkJsonObjectW_stringOf(json,L"reader[i].vendorName");
        systemName = CkJsonObjectW_stringOf(json,L"reader[i].systemName");
        cardAtr = CkJsonObjectW_stringOf(json,L"reader[i].card.atr");
        cardWindowsMiniDriver = CkJsonObjectW_stringOf(json,L"reader[i].card.windows.miniDriver");
        cardWindowsCryptoProvider = CkJsonObjectW_stringOf(json,L"reader[i].card.windows.cryptoProvider");
        cardWindowsKeyStorageProvider = CkJsonObjectW_stringOf(json,L"reader[i].card.windows.keyStorageProvider");
        serialNumber = CkJsonObjectW_stringOf(json,L"reader[i].serialNumber");
        i = i + 1;
    }

    // Applications should always release the context when finished.
    success = CkSCardW_ReleaseContext(scard);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSCardW_lastErrorText(scard));
    }



    CkSCardW_Dispose(scard);
    CkJsonObjectW_Dispose(json);

    }