windows 使用 WinSCard 获取 PCSC 阅读器序列号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6940824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:41:14  来源:igfitidea点击:

Getting PCSC reader serial number with WinSCard

windowsserial-numberpcscwinscard

提问by vellotis

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++.

如果读卡器中没有卡,我在获取 PCSC 读卡器序列号时会遇到问题。我正在使用 winscard.dll 和 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。我还没有找到任何其他方式来获得 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?

有没有办法在不连接卡的情况下获取读卡器序列号?

采纳答案by mtraut

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

您可以通过 SCardConnect 使用 SCARD_SHARE_DIRECT 标志直接连接到读卡器。至少对我们来说这很好用..(我们使用“0x00”的协议标志)

回答by now he who must not be named.

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 szAvailRdrrefers to the reader name (smartcard readername) and hCardHandleis a handle obtained before using scardconnect.

whereszAvailRdr指的是读卡器名称(智能卡读卡器名称),hCardHandle是使用之前获得的句柄scardconnect

This should keep you going!

这应该会让你继续前进!