Getting PCSC reader serial number with WinSCard

2019-02-17 15:59发布

问题:

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   pbAttr = NULL;
DWORD    cByte = SCARD_AUTOALLOCATE;

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

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

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

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

回答1:

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")



回答2:

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!