diff pcsc-test/pcsc-test1.c @ 84:1d7d8615d628

pcsc-test: some experiments to get pcsc-lite working
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 23 Jan 2021 17:21:08 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pcsc-test/pcsc-test1.c	Sat Jan 23 17:21:08 2021 +0000
@@ -0,0 +1,59 @@
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+
+main(argc, argv)
+	char **argv;
+{
+	SCARDCONTEXT hContext;
+	LONG rv;
+	DWORD dwReaders;
+	LPSTR mszReaders;
+	char *cp;
+
+	rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
+	if (rv != SCARD_S_SUCCESS) {
+		fprintf(stderr, "SCardEstablishContext: %s\n",
+			pcsc_stringify_error(rv));
+		exit(1);
+	}
+	rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
+	if (rv != SCARD_S_SUCCESS) {
+		fprintf(stderr, "SCardListReaders 1st call: %s\n",
+			pcsc_stringify_error(rv));
+		SCardReleaseContext(hContext);
+		exit(1);
+	}
+	printf("dwReaders = %u\n", (unsigned) dwReaders);
+	if (dwReaders < 1) {
+		fprintf(stderr,
+	"error: dwReaders returned by SCardListReaders() is less than 1\n");
+		SCardReleaseContext(hContext);
+		exit(1);
+	}
+	mszReaders = malloc(dwReaders);
+	if (!mszReaders) {
+		perror("malloc for readers list");
+		SCardReleaseContext(hContext);
+		exit(1);
+	}
+	*mszReaders = '\0';
+	rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders);
+	if (rv != SCARD_S_SUCCESS) {
+		fprintf(stderr, "SCardListReaders 2nd call: %s\n",
+			pcsc_stringify_error(rv));
+		SCardReleaseContext(hContext);
+		exit(1);
+	}
+	for (cp = mszReaders; *cp; ) {
+		printf("Reader: %s\n", cp);
+		cp += strlen(cp) + 1;
+	}
+	printf("End of list of readers\n");
+	SCardReleaseContext(hContext);
+	exit(0);
+}