[Winpt-commits] r342 - trunk/Src

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Nov 27 14:16:39 CET 2011


Author: twoaday
Date: 2011-11-27 14:16:39 +0100 (Sun, 27 Nov 2011)
New Revision: 342

Added:
   trunk/Src/wptCardPCSC.cpp
Log:


Added: trunk/Src/wptCardPCSC.cpp
===================================================================
--- trunk/Src/wptCardPCSC.cpp	2011-11-27 13:15:19 UTC (rev 341)
+++ trunk/Src/wptCardPCSC.cpp	2011-11-27 13:16:39 UTC (rev 342)
@@ -0,0 +1,94 @@
+/* wptCardPCSC.cpp - PC/SC card API interface
+ *	Copyright (C) 2003, 2006 Timo Schulz
+ *
+ * This file is part of WinPT.
+ *
+ * WinPT is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * WinPT is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <windows.h>
+#ifndef __MINGW32__
+#include <winscard.h>
+#else
+/* mingw32 has no win32 smartcard API definitions. */
+typedef ULONG_PTR SCARDCONTEXT;
+#define SCARD_SCOPE_SYSTEM 2
+#define SCARD_S_SUCCESS NO_ERROR
+#endif
+
+#include "wptTypes.h"
+#include "wptCard.h"
+
+#define load_dll_fnc(cast, name) (cast)GetProcAddress(hlib, name)
+
+
+typedef LONG (WINAPI *pcsc_establish_context_t) (unsigned long scope,
+                                const void *reserved1,
+                                const void *reserved2,
+                                unsigned long *r_context);
+typedef LONG (WINAPI *pcsc_release_context_t) (unsigned long context);
+
+static pcsc_establish_context_t pcsc_establish = NULL;
+static pcsc_release_context_t pcsc_release = NULL;
+static int pcsc_init = 0;
+
+
+static int
+pcsc_loadlib (void)
+{
+    HMODULE hlib;
+
+    if (pcsc_init)
+	return 0;
+    hlib = LoadLibrary ("WINSCARD");
+    if (!hlib)
+	goto fail;
+    pcsc_establish = load_dll_fnc (pcsc_establish_context_t, 
+				   "SCardEstablishContext");
+    pcsc_release = load_dll_fnc (pcsc_release_context_t, 
+				 "SCardReleaseContext");
+    if (pcsc_establish != NULL && pcsc_release != NULL)
+	goto success;
+
+fail:
+    if (hlib != NULL)
+	FreeLibrary (hlib);
+    return -1;
+
+success:
+    pcsc_init = 1;
+    return 0;
+}
+
+
+/* Return != 0 if the PC/SC smart card interface is available.
+   The purpose of the function is to disable smart card releation
+   commands on systems with no scard readers. */
+int
+pcsc_available (void)
+{
+    SCARDCONTEXT ctx;
+    LONG err;
+
+    if (pcsc_loadlib ())
+	return 0;
+
+    err = pcsc_establish (SCARD_SCOPE_SYSTEM, NULL, NULL, &ctx);
+    if (err == SCARD_S_SUCCESS) {
+	pcsc_release (ctx);
+	return -1;
+    }
+    return 0;
+}



More information about the Winpt-commits mailing list