Sample code for 30+ languages & platforms
PowerBuilder

PC/SC Check the Status of each Smart Card Reader (and USB token)

See more SCard Examples

Demonstrates how to list the smart card readers and USB tokens connected to the computer, and check the status of each.

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_StReaders
integer li_NumReaders
integer i
string ls_Reader

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

loo_StReaders = create oleobject
li_rc = loo_StReaders.ConnectToNewObject("Chilkat.StringTable")

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

li_NumReaders = loo_StReaders.Count
i = 0
do while i < li_NumReaders
    ls_Reader = loo_StReaders.StringAt(i)

    Write-Debug string(i) + ": " + ls_Reader

    // Connect to this reader.
    if loo_Scard.Connect(ls_Reader,"shared","no_preference") = 1 then

        // Check the status to get the reader state, active protocol, and card ATR (if a card is inserted).
        if loo_Scard.CheckStatus() = 1 then
            Write-Debug "ReaderStatus: " + loo_Scard.ReaderStatus
            Write-Debug "CardAtr: " + loo_Scard.CardAtr
            Write-Debug "ActiveProtocol: " + loo_Scard.ActiveProtocol
        else
            Write-Debug loo_Scard.LastErrorText
            Write-Debug "CheckStatus failed."
        end if

        // Disconnect from this reader.
        li_Success = loo_Scard.Disconnect("leave")
    else
        Write-Debug loo_Scard.LastErrorText
        Write-Debug "Connect failed."
    end if

    Write-Debug "---------------------------------------"
    i = i + 1
loop

// Sample output:

// 0: Alcor Micro USB Smart Card Reader 0
// ReaderStatus: specific
// CardAtr: 3B7F96000080318065B0855956FB120FFE829000
// ActiveProtocol: T0
// ---------------------------------------
// 1: Generic Smart Card Reader Interface 0
// ReaderStatus: specific
// CardAtr: 3BFC180000813180459067464A00641606F2727E00E0
// ActiveProtocol: T1
// ---------------------------------------
// 2: SCM Microsystems Inc. SCR33x USB Smart Card Reader 0
// ReaderStatus: specific
// CardAtr: 3BDF96FF8131FE455A018048494443313158587300011B09
// ActiveProtocol: T1
// ---------------------------------------

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