我正在实施一个PKCS#11库,它将使用Windows中的 winscard 和MAC中的 PCSC 与读卡器进行通信。
我使用以下示例代码从读者获取卡片更改状态。 它在MAC中工作正常,并为dwEventState返回0x0122。 但是在windows SCardGetStatusChange 总是返回6并且 dwEventState 没有改变。
虽然SCardConnect成功连接到Windows中的阅读器并返回卡片句柄。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __APPLE__
#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#else
#include <winscard.h>
#endif
int printResult (char *s, int rv){
printf("%s %x \n", s, rv);
return 0;
}
int main(void)
{
LONG rv;
SCARDCONTEXT hContext;
LPTSTR mszReaders , pReader;
SCARDHANDLE hCard;
DWORD dwReaders, dwActiveProtocol;
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
printResult("SCardEstablishContext", rv);
#ifdef SCARD_AUTOALLOCATE
dwReaders = SCARD_AUTOALLOCATE;
rv = SCardListReaders(hContext, NULL, (LPTSTR)&mszReaders, &dwReaders);
printResult("SCardListReaders", rv);
#else
rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
printResult("SCardListReaders", rv)
mszReaders = calloc(dwReaders, sizeof(char));
rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders);
printResult("SCardListReaders", rv)
#endif
int count = 0;
switch( rv )
{
case SCARD_E_NO_READERS_AVAILABLE:
printf("Reader is not in groups.\n");
// Take appropriate action.
// ...
break;
case SCARD_S_SUCCESS:
{
pReader = mszReaders;
while ( 0 != *pReader )
{
++count;
printf("Reader: %s\n", pReader );
rv = SCardConnect(hContext, pReader, SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol);
printResult("SCardConnect", rv);
if (rv == SCARD_S_SUCCESS){
SCARD_READERSTATE rgReaderStates;
rgReaderStates.dwEventState = 0;
rgReaderStates.dwCurrentState = SCARD_STATE_UNAWARE;
rgReaderStates.szReader = pReader;
// rgReaderStates.szReader = "OMNIKEY CardMan 3x21 0";
rv = SCardGetStatusChange(hCard, 0, &rgReaderStates, 2);
printf("CurrentState : 0x%08lX Event State : 0x%08lX RV=%ld\n", rgReaderStates.dwCurrentState, rgReaderStates.dwEventState, rv);
}
// Advance to the next value.
pReader = pReader + strlen(pReader) + 1;
}
printf("Number of readers %d\n",count);
// printf("reader name: %s\n", mszReaders[1]);
// Free the memory.
break;
}
default:
printf("Failed SCardListReaders\n");
// Take appropriate action.
// ...
break;
}
rv = SCardDisconnect(hCard, SCARD_LEAVE_CARD);
printResult("SCardDisconnect", rv);
#ifdef SCARD_AUTOALLOCATE
rv = SCardFreeMemory(hContext, mszReaders);
printResult("SCardFreeMemory", rv);
#else
free(mszReaders);
#endif
rv = SCardReleaseContext(hContext);
printResult("SCardReleaseContext", rv);
return 0;
}
代码输出如下
SCardEstablishContext 0
SCardListReaders 0
Reader: OMNIKEY CardMan 3x21 0
SCardConnect 0
CurrentState : 0x00000000 Event State : 0x00000000 RV=6
Reader: OMNIKEY CardMan 3x21 1
SCardConnect 0
CurrentState : 0x00000000 Event State : 0x00000000 RV=6
Number of readers 2
SCardDisconnect 0
SCardFreeMemory 0
SCardReleaseContext 0
请帮忙。
答案 0 :(得分:0)
我的来源有误
rv = SCardGetStatusChange(hCard, 0, &rgReaderStates, 2);
hCard 应替换为 hContext
rv = SCardGetStatusChange(hContext, 0, &rgReaderStates, 2);
不知道为什么它在MAC上提供准确的输出