Zig
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const scard = try chilkat.SCard.init();
defer scard.deinit();
// First establish a context to the PC/SC Resource Manager
scard.establishContext("user") catch {
std.debug.print("{s}\n", .{try scard.getLastErrorText(alloc)});
return;
};
const st_readers = try chilkat.StringTable.init();
defer st_readers.deinit();
scard.listReaders(st_readers) catch {
std.debug.print("{s}\n", .{try scard.getLastErrorText(alloc)});
return;
};
const num_readers = st_readers.getCount();
var i: i32 = 0;
while (i < num_readers) {
const reader = try st_readers.stringAt(alloc, i);
std.debug.print("{d}: {s}\n", .{ i, reader });
// Connect to this reader.
if (scard.connect(reader, "shared", "no_preference")) {
// Check the status to get the reader state, active protocol, and card ATR (if a card is inserted).
if (scard.checkStatus()) {
std.debug.print("ReaderStatus: {s}\n", .{try scard.getReaderStatus(alloc)});
std.debug.print("CardAtr: {s}\n", .{try scard.getCardAtr(alloc)});
std.debug.print("ActiveProtocol: {s}\n", .{try scard.getActiveProtocol(alloc)});
} else {
std.debug.print("{s}\n", .{try scard.getLastErrorText(alloc)});
std.debug.print("CheckStatus failed.\n", .{});
}
// Disconnect from this reader.
try scard.disconnect("leave");
} else {
std.debug.print("{s}\n", .{try scard.getLastErrorText(alloc)});
std.debug.print("Connect failed.\n", .{});
}
std.debug.print("---------------------------------------\n", .{});
i = i + 1;
}
// 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.
scard.releaseContext() catch {
std.debug.print("{s}\n", .{try scard.getLastErrorText(alloc)});
};
}