Sample code for 30+ languages & platforms
Xbase++

PC/SC Get Card UID

See more SCard Examples

Sends the APDU command to get a card's UID.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oScard
LOCAL oBdRecv
LOCAL nNumBytes

nSuccess := 0

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

oScard := CreateObject("Chilkat.SCard")

//  First establish a context to the PC/SC Resource Manager
nSuccess := oScard:EstablishContext("user")
IF (nSuccess == 0)
    ? oScard:LastErrorText
    oScard:destroy()
    RETURN
ENDIF

//  Use your own smart card reader name here.
nSuccess := oScard:Connect("ACS ACR122 0", "shared", "no_preference")
IF (nSuccess == 0)
    ? oScard:LastErrorText
    oScard:destroy()
    RETURN
ENDIF

? "Connected reader: " + oScard:ConnectedReader
? "Active protocol: " + oScard:ActiveProtocol
? "ATR: " + oScard:CardAtr
? "Reader Status: " + oScard:ReaderStatus

//   Send the APDU command 0xFF, 0xCA, 0x00, 0x00, 0x00
oBdRecv := CreateObject("Chilkat.BinData")
nSuccess := oScard:TransmitHex(oScard:ActiveProtocol, "FFCA000000", oBdRecv, 32)
IF (nSuccess == 1)

    ? "Received: " + oBdRecv:GetEncoded("hex")

    //  The UID is the returned data without the final 2 bytes.
    nNumBytes := oBdRecv:NumBytes
    IF (nNumBytes > 2)
        ? "UID: " + oBdRecv:GetEncodedChunk(0, nNumBytes - 2, "hex")
    ENDIF

ELSE
    ? oScard:LastErrorText
ENDIF

//  Disconnect from this reader.
nSuccess := oScard:Disconnect("leave")
IF (nSuccess == 0)
    ? oScard:LastErrorText
ENDIF

//  Applications should always release the context when finished.
nSuccess := oScard:ReleaseContext()
IF (nSuccess == 0)
    ? oScard:LastErrorText
ENDIF

oScard:destroy()
oBdRecv:destroy()