I bought a cheap Smartcard Reader online and want to make an Attendance System which would mark attendance of Employees when they insert their civil ID Cards.
I'm really a noobie in Development and this is my first stackoverflow question. Please bear with me if i'm irritating :p
Here's what I did in C++
#include <iostream>
#include <WinScard.h>
#include <SCardErr.h>
using namespace std;
int main()
{
SCARDCONTEXT hSC;
LONG lReturn;
lReturn = SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL,&hSC);
if (SCARD_S_SUCCESS != lReturn)
printf("Failed SCardEstablishContext\n");
else
{
printf("Yay\n");
LPTSTR pmszReaders = NULL;
LPTSTR pReader = NULL;
LONG lReturn, lReturn2;
DWORD cch = SCARD_AUTOALLOCATE;
LPCWSTR ReaderFound = NULL;
// Retrieve the list the readers.
// hSC was set by a previous call to SCardEstablishContext.
lReturn = SCardListReaders(hSC,
NULL,
(LPTSTR)&pmszReaders,
&cch);
int count = 0;
switch (lReturn)
{
case SCARD_E_NO_READERS_AVAILABLE:
printf("Reader is not in groups.\n");
// Take appropriate action.
// ...
break;
case SCARD_S_SUCCESS:
// Do something with the multi string of readers.
// Output the values.
// A double-null terminates the list of values.
pReader = pmszReaders;
ReaderFound = pmszReaders;
while ('\0' != *pReader)
{
// Display the value.
printf("Reader: %S\n", pReader);
// Advance to the next value.
pReader = pReader + wcslen((wchar_t*)pReader) + 1;
count++;
}
// Free the memory.
lReturn2 = SCardFreeMemory(hSC,
pmszReaders);
if (SCARD_S_SUCCESS != lReturn2)
printf("Failed SCardFreeMemory\n");
break;
default:
printf("Failed SCardListReaders\n");
// Take appropriate action.
// ...
break;
}
SCARDHANDLE hCardHandle;
DWORD dwAP;
lReturn = SCardConnect(
hSC,
ReaderFound,
SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
&hCardHandle,
&dwAP);
if (SCARD_S_SUCCESS != lReturn)
{
//printf("Failed SCardConnect\n");
//cout << "Reader: " << pReader[0] << endl;
//cout << "Reader: " << pmszReaders << endl;
wprintf(L"Failed to connect to card in reader '%s' with error 0x%x.\n", ReaderFound, lReturn);
system("pause");
exit(1); // Or other appropriate action.
}
// Use the connection.
// Display the active protocol.
switch (dwAP)
{
case SCARD_PROTOCOL_T0:
printf("Active protocol T0\n");
break;
case SCARD_PROTOCOL_T1:
printf("Active protocol T1\n");
break;
case SCARD_PROTOCOL_UNDEFINED:
default:
printf("Active protocol unnegotiated or unknown\n");
break;
}
// Remember to disconnect (by calling SCardDisconnect).
// ...
}
system("pause");
return 0;
}
Here's the New Output
Yay Reader: Generic Smart Card Reader Interface 0 Active protocol T0 Press any key to continue . . .
User contributions licensed under CC BY-SA 3.0