Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting PCSC reader serial number with WinSCard

I have a problem with getting PCSC reader serial number if card is not present in the reader. I am using winscard.dll and c++.

The following code will work only for the case if card is present in the reader. Otherwise the SCardHandle is not retrieved. I haven't found any other way to get SCardHandle.

SCARDHANDLE hCardHandle;
SCARDCONTEXT    hSC;
WCHAR   pCardReaderName[256];
LONG lReturn;

lReturn = SCardEstablishContext(SCARD_SCOPE_USER, 0, 0, &hSC);

if (lReturn != SCARD_S_SUCCESS)
{
    Console::WriteLine("SCardEstablishContext() failed\n");
    return;
}

my_select_reader(hSC, pCardReaderName); // just shows reader names in console and requires you to pick one

// connect to smart card
DWORD   dwAP;

lReturn = SCardConnect( hSC,
                (LPCWSTR)pCardReaderName,
                SCARD_SHARE_SHARED,
                SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 | SCARD_PROTOCOL_RAW,
                &hCardHandle,
                &dwAP );

if ( SCARD_S_SUCCESS != lReturn )
{
    Console::WriteLine("Failed SCardConnect\n");
    exit(1);  // Or other appropriate action.
}

// get reader serial no
LPBYTE   pbAtr = NULL;
DWORD    cByte = SCARD_AUTOALLOCATE;

lReturn = SCardGetAttrib(hCardHandle,
                SCARD_ATTR_VENDOR_IFD_SERIAL_NO,
                (LPBYTE)&pbAtr,
                &cByte);

if ( SCARD_S_SUCCESS != lReturn )
{
    Console::WriteLine("Failed to retrieve Reader Serial\n");
    exit(1);  // Or other appropriate action.
}

printf("serial no: %s", pbAtr);

SCardFreeMemory(hCardHandle, pbAtr); 

Is there a way to get readers serial number without connecting to card?

like image 565
vellotis Avatar asked Aug 04 '11 11:08

vellotis


2 Answers

Maybe i'm a bit late - but anyway...

You can connect directly to the card reader using the SCARD_SHARE_DIRECT flag with SCardConnect. At least with us this works fine.. (we use a protocol flag of "0x00")

like image 61
mtraut Avatar answered Oct 22 '22 22:10

mtraut


You should be using:

lReturn = SCardConnect(hResManager,szAvailRdr,SCARD_SHARE_SHARED,SCARD_PROTOCOL_T1,
                            &hCardHandle,
                            &dwActProtocol);

Instead, try using:

lReturn = SCardConnect(hResManager,szAvailRdr,SCARD_SHARE_DIRECT,
                      NULL,
                      &hCardHandle,
                      NULL);

where szAvailRdr refers to the reader name (smartcard readername) and hCardHandle is a handle obtained before using scardconnect.

This should keep you going!

like image 4
now he who must not be named. Avatar answered Oct 23 '22 00:10

now he who must not be named.