comparison pcsc/context.c @ 0:f4479a0d4cea

fc-pcsc-list ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 00:45:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f4479a0d4cea
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pcsclite.h>
4 #include <winscard.h>
5
6 SCARDCONTEXT hContext;
7 char *reader_list;
8
9 setup_pcsc_context()
10 {
11 LONG rv;
12
13 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
14 if (rv != SCARD_S_SUCCESS) {
15 fprintf(stderr, "SCardEstablishContext: %s\n",
16 pcsc_stringify_error(rv));
17 exit(1);
18 }
19 return(0);
20 }
21
22 get_reader_list()
23 {
24 LONG rv;
25 DWORD dwReaders;
26
27 rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
28 if (rv != SCARD_S_SUCCESS) {
29 fprintf(stderr, "SCardListReaders 1st call: %s\n",
30 pcsc_stringify_error(rv));
31 SCardReleaseContext(hContext);
32 exit(1);
33 }
34 if (dwReaders < 1) {
35 fprintf(stderr,
36 "error: dwReaders returned by SCardListReaders() is less than 1\n");
37 SCardReleaseContext(hContext);
38 exit(1);
39 }
40 reader_list = malloc(dwReaders);
41 if (!reader_list) {
42 perror("malloc for readers list");
43 SCardReleaseContext(hContext);
44 exit(1);
45 }
46 reader_list[0] = '\0';
47 rv = SCardListReaders(hContext, NULL, reader_list, &dwReaders);
48 if (rv != SCARD_S_SUCCESS) {
49 fprintf(stderr, "SCardListReaders 2nd call: %s\n",
50 pcsc_stringify_error(rv));
51 SCardReleaseContext(hContext);
52 exit(1);
53 }
54 return(0);
55 }