|
// SCardDatabaseEx.cpp : Implementation of CSCardDatabaseEx
#include "stdafx.h"
#include "SCardDatabaseEx.h"
#include ".\scarddatabaseex.h"
#include <winscard.h>
// CSCardDatabaseEx
STDMETHODIMP CSCardDatabaseEx::ListReaders(VARIANT* ppReaders)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (ppReaders == NULL)
return E_POINTER;
LONG lErr = -1;
SCARDCONTEXT hSC;
LPTSTR pmszReaders = NULL;
LPTSTR pReader;
DWORD cch = SCARD_AUTOALLOCATE;
CStringArray l_strReaderList;
bool l_bReaderFound = false;
int l_nNbReader = 0;
HRESULT l_hRes = E_POINTER;
SAFEARRAYBOUND l_bound[1];
SAFEARRAY *psa;
long l_nIx[1];
// 1 - Get reader list to know how many readers are present
lErr = SCardEstablishContext(SCARD_SCOPE_USER,
NULL,
NULL,
&hSC );
if (SCARD_S_SUCCESS == lErr)
{
pmszReaders = NULL;
cch = SCARD_AUTOALLOCATE;
lErr = SCardListReaders(hSC,
NULL,
(LPTSTR)&pmszReaders,
&cch );
//ATLTRACE("SCardListReaders returned 0x%08X\r\n", lRet2);
if (lErr == SCARD_S_SUCCESS)
{
// Get the list of readers
// Do something with the multi string of readers.
// Here, we'll merely output the values.
// A double-null terminates the list of values.
pReader = pmszReaders;
while ( '\0' != *pReader )
{
l_strReaderList.Add(pReader);
// Increase reader number
l_nNbReader++;
// Advance to the next value.
pReader = pReader + strlen(pReader) + 1;
}
}
// Free the memory.
lErr = SCardFreeMemory( hSC,
pmszReaders );
}
SCardReleaseContext(hSC);
l_bound[0].lLbound = 0;
l_bound[0].cElements = l_nNbReader;
psa = SafeArrayCreate(VT_BSTR, 1, l_bound);
for (int nI = 0; nI < l_nNbReader; nI++)
{
l_nIx[0] = nI;
l_hRes = SafeArrayPutElement(psa, l_nIx, l_strReaderList.GetAt(nI).AllocSysString());
if (FAILED(l_hRes))
return l_hRes;
}
ppReaders->vt = VT_ARRAY | VT_BSTR;
ppReaders->parray = psa;
l_hRes = S_OK;
return l_hRes;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Software Architect, COM, .NET and Smartcard based security specialist.
I've been working in the software industry since I graduated in Electrical and Electronics Engineering. I chose software because I preferred digital to analog.
I started to program with 6802 machine code and evolved to the current .NET technologies... that was a long way.
For more than 20 years I have always worked in technical positions as I simply like to get my hands dirty and crack my brain when things don't go right!
After 12 years in the smart card industry I can claim a strong knowledge in security solutions based on those really small computers!
I've been back into business to design the licensing system for the enterprise solution for Consistel using a .NET smart card (yes they can run .NET CLR!)
I'm currently designing a micro-payment solution using the NXP DESFire EV1 with the ACSO6 SAM of ACS. I can then add a full proficient expertise on those systems and NFC payments.
This technology being under strict NDA by NXP I cannot publish any related article about it, however I can provide professional consulting for it.
You can contact me for professional matter by using the forum or via my
LinkedIn profile.