Getting PCSC reader serial number with WinSCard

纵饮孤独 提交于 2019-12-01 02:54:29

问题


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?


回答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!



来源:https://stackoverflow.com/questions/6940824/getting-pcsc-reader-serial-number-with-winscard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!