Go to the documentation of this file.
50 #ifndef MAXHOSTNAMELEN
51 #define MAXHOSTNAMELEN 64
54 int main(
int argc,
char *argv[])
60 DWORD dwSendLength, dwRecvLength, dwPref, dwReaders;
61 LPSTR mszReaders = NULL;
67 int i, p, iReader, cnum, iProtocol;
69 char pcHost[MAXHOSTNAMELEN];
70 char pcAFile[FILENAME_MAX];
71 char pcOFile[FILENAME_MAX];
76 printf(
"\nWinscard PC/SC Lite Test Program\n\n");
78 printf(
"Please enter the desired host (localhost for this machine) [localhost]: ");
79 (void)fgets(line,
sizeof(line), stdin);
81 strncpy(pcHost,
"localhost",
sizeof(pcHost)-1);
83 strncpy(pcHost, line,
sizeof(pcHost)-1);
85 printf(
"Please input the desired transmit protocol (0/1) [0]: ");
86 (void)fgets(line,
sizeof(line), stdin);
90 (
void)sscanf(line,
"%d", &iProtocol);
92 printf(
"Please input the desired input apdu file: ");
93 (void)fgets(line,
sizeof(line), stdin);
94 (void)sscanf(line,
"%s", pcAFile);
96 printf(
"Please input the desired output apdu file: ");
97 (void)fgets(line,
sizeof(line), stdin);
98 (void)sscanf(line,
"%s", pcOFile);
100 fp = fopen(pcAFile,
"r");
107 fo = fopen(pcOFile,
"w");
119 printf(
"ERROR :: Cannot Connect to Resource Manager\n");
129 printf(
"SCardListReaders error line %d: %08X\n", __LINE__, rv);
132 mszReaders = malloc(
sizeof(
char) * dwReaders);
136 printf(
"SCardListReaders error line %d: %08X\n", __LINE__, rv);
144 for (i = 0; i < dwReaders - 1; i++)
147 printf(
"Reader %02d: %s\n", p, &mszReaders[i]);
149 while (mszReaders[++i] != 0) ;
154 printf(
"Enter the desired reader number: ");
155 (void)fgets(line,
sizeof(line), stdin);
156 (void)sscanf(line,
"%d", &iReader);
159 if (iReader > p || iReader <= 0)
161 printf(
"Invalid Value - try again\n");
164 while (iReader > p || iReader <= 0);
166 rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
169 printf(
"Please insert a smart card\n");
173 printf(
"SCardGetStatusChange error line %d: %08X\n", __LINE__, rv);
177 rv =
SCardConnect(hContext, &mszReaders[iList[iReader]],
182 printf(
"SCardConnect error line %d: %08X\n", __LINE__, rv);
193 printf(
"SCardBeginTransaction error line %d: %08X\n", __LINE__, rv);
203 if (fgets(line,
sizeof(line), fp) == NULL)
214 if (sscanf(line_ptr,
"%x", &x) == 0)
218 line_ptr = strchr(line_ptr,
' ');
219 if (line_ptr == NULL)
223 for (i = 0; i < dwSendLength; i++)
225 if (sscanf(line_ptr,
"%x", &x) == 0)
227 printf(
"Corrupt APDU: %s\n", line);
233 line_ptr = strchr(line_ptr,
' ');
235 if (line_ptr == NULL)
241 printf(
"Processing Command %03d of length %03lX: ", cnum,
243 for (i=0; i<dwSendLength; i++)
244 printf(
"%02X ", s[i]);
253 &sRecvPci, r, &dwRecvLength);
260 &sRecvPci, r, &dwRecvLength);
264 printf(
"Invalid Protocol\n");
271 fprintf(fo,
".error 0x%08lX\n", rv);
272 printf(
"Error: 0x%08lX\n", rv);
276 fprintf(fo,
"%02ld ", dwRecvLength);
277 printf(
"Received %ld bytes: ", dwRecvLength);
279 for (i = 0; i < dwRecvLength; i++)
281 fprintf(fo,
"%02X ", r[i]);
282 printf(
"%02X ", r[i]);
296 printf(
"SCardReconnect error line %d: %08X\n", __LINE__, rv);
PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext)
Destroys a communication context to the PC/SC Resource Manager.
PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
Terminates a connection made through SCardConnect().
#define SCARD_RESET_CARD
Reset on close.
#define SCARD_S_SUCCESS
No error was encountered.
PCSC_API LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
Creates an Application Context to the PC/SC Resource Manager.
#define SCARD_SHARE_SHARED
Shared mode only.
LONG SCARDHANDLE
hCard returned by SCardConnect()
#define SCARD_SCOPE_USER
Scope in user space.
PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard)
Establishes a temporary exclusive access mode for doing a serie of commands in a transaction.
#define SCARD_PROTOCOL_T0
T=0 active protocol.
#define SCARD_W_RESET_CARD
The smart card has been reset, so any shared state information is invalid.
#define SCARD_PROTOCOL_T1
T=1 active protocol.
#define INFINITE
Infinite timeout.
#define SCARD_UNPOWER_CARD
Power down on close.
#define SCARD_PCI_T1
protocol control information (PCI) for T=1
This handles smart card reader communications.
PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol)
Reestablishes a connection to a reader that was previously connected to using SCardConnect().
LONG SCARDCONTEXT
hContext returned by SCardEstablishContext()
#define SCARD_LEAVE_CARD
Do nothing on close.
#define MAX_BUFFER_SIZE
Maximum Tx/Rx Buffer for short APDU.
PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
Returns a list of currently available readers on the system.
Protocol Control Information (PCI)
#define SCARD_PCI_T0
protocol control information (PCI) for T=0
PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition)
Ends a previously begun transaction.
PCSC_API LONG SCardGetStatusChange(SCARDCONTEXT hContext, DWORD dwTimeout, SCARD_READERSTATE *rgReaderStates, DWORD cReaders)
Blocks execution until the current availability of the cards in a specific set of readers changes.
#define SCARD_STATE_EMPTY
Card removed.
This keeps a list of Windows(R) types.
PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
Establishes a connection to the reader specified in * szReader.
PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, const SCARD_IO_REQUEST *pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST *pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength)
Sends an APDU to the smart card contained in the reader connected to by SCardConnect().