Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Scard
oleobject loo_Json
string ls_Name
string ls_State
string ls_VendorName
string ls_SystemName
string ls_CardAtr
string ls_CardWindowsMiniDriver
string ls_CardWindowsCryptoProvider
string ls_CardWindowsKeyStorageProvider
string ls_SerialNumber
integer i
integer li_Count_i

li_Success = 0

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

loo_Scard = create oleobject
li_rc = loo_Scard.ConnectToNewObject("Chilkat.SCard")
if li_rc < 0 then
    destroy loo_Scard
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// First establish a context to the PC/SC Resource Manager
li_Success = loo_Scard.EstablishContext("user")
if li_Success = 0 then
    Write-Debug loo_Scard.LastErrorText
    destroy loo_Scard
    return
end if

// Get JSON containing information about the smartcards currently inserted into readers.
// This also includes information about USB security tokens.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Scard.FindSmartcards(loo_Json)
if li_Success = 0 then
    Write-Debug loo_Scard.LastErrorText
    destroy loo_Scard
    destroy loo_Json
    return
end if

loo_Json.EmitCompact = 0
Write-Debug loo_Json.Emit()

// 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

i = 0
li_Count_i = loo_Json.SizeOfArray("reader")
do while i < li_Count_i
    loo_Json.I = i
    ls_Name = loo_Json.StringOf("reader[i].name")
    ls_State = loo_Json.StringOf("reader[i].state")
    ls_VendorName = loo_Json.StringOf("reader[i].vendorName")
    ls_SystemName = loo_Json.StringOf("reader[i].systemName")
    ls_CardAtr = loo_Json.StringOf("reader[i].card.atr")
    ls_CardWindowsMiniDriver = loo_Json.StringOf("reader[i].card.windows.miniDriver")
    ls_CardWindowsCryptoProvider = loo_Json.StringOf("reader[i].card.windows.cryptoProvider")
    ls_CardWindowsKeyStorageProvider = loo_Json.StringOf("reader[i].card.windows.keyStorageProvider")
    ls_SerialNumber = loo_Json.StringOf("reader[i].serialNumber")
    i = i + 1
loop

// Applications should always release the context when finished.
li_Success = loo_Scard.ReleaseContext()
if li_Success = 0 then
    Write-Debug loo_Scard.LastErrorText
end if



destroy loo_Scard
destroy loo_Json