Unicode C
Unicode C
PC/SC Get Card UID
See more SCard Examples
Sends the APDU command to get a card's UID.Chilkat Unicode C Downloads
#include <C_CkSCardW.h>
#include <C_CkBinDataW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSCardW scard;
HCkBinDataW bdRecv;
int numBytes;
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;
}
// Use your own smart card reader name here.
success = CkSCardW_Connect(scard,L"ACS ACR122 0",L"shared",L"no_preference");
if (success == FALSE) {
wprintf(L"%s\n",CkSCardW_lastErrorText(scard));
CkSCardW_Dispose(scard);
return;
}
wprintf(L"Connected reader: %s\n",CkSCardW_connectedReader(scard));
wprintf(L"Active protocol: %s\n",CkSCardW_activeProtocol(scard));
wprintf(L"ATR: %s\n",CkSCardW_cardAtr(scard));
wprintf(L"Reader Status: %s\n",CkSCardW_readerStatus(scard));
// Send the APDU command 0xFF, 0xCA, 0x00, 0x00, 0x00
bdRecv = CkBinDataW_Create();
success = CkSCardW_TransmitHex(scard,CkSCardW_activeProtocol(scard),L"FFCA000000",bdRecv,32);
if (success == TRUE) {
wprintf(L"Received: %s\n",CkBinDataW_getEncoded(bdRecv,L"hex"));
// The UID is the returned data without the final 2 bytes.
numBytes = CkBinDataW_getNumBytes(bdRecv);
if (numBytes > 2) {
wprintf(L"UID: %s\n",CkBinDataW_getEncodedChunk(bdRecv,0,numBytes - 2,L"hex"));
}
}
else {
wprintf(L"%s\n",CkSCardW_lastErrorText(scard));
}
// Disconnect from this reader.
success = CkSCardW_Disconnect(scard,L"leave");
if (success == FALSE) {
wprintf(L"%s\n",CkSCardW_lastErrorText(scard));
}
// 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);
CkBinDataW_Dispose(bdRecv);
}