我正在尝试通过Windows智能卡api连接并向智能卡发送命令,下面是我从MSDN获得的代码。这里的SCardTransmit函数总是返回0x000005aa,有人可以让我知道我做错了什么。
int _tmain(int argc, _TCHAR* argv[])
{
SCARDCONTEXT hContext;
LONG lReturn;
SCARDHANDLE hCardHandle;
DWORD dwAP;
const DWORD BUF_LEN = 512;
DWORD dwRecv;
BYTE pbRecv[BUF_LEN];
BYTE sendCommand[]={0x00,0x0A4,0x00,0x00,0x02,0x3F,0x00};
lReturn = SCardEstablishContext(SCARD_SCOPE_USER, NULL,NULL,&hContext);
if ( SCARD_S_SUCCESS != lReturn )
{
printf("Failed SCardEstablishContext\n");
SCardReleaseContext(hContext);
return 0;
}
lReturn = SCardConnect( hContext, "OMNIKEY CardMan 3x21 0",
SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
&hCardHandle, &dwAP );
if ( SCARD_S_SUCCESS != lReturn )
{
printf("Failed SCardConnect\n");
SCardReleaseContext(hContext);
return 0;
}
switch ( dwAP )
{
case SCARD_PROTOCOL_T0:
printf("Active protocol T0\n");
break;
case SCARD_PROTOCOL_T1:
printf("Active protocol T1\n");
break;
case SCARD_PROTOCOL_UNDEFINED:
default:
printf("Active protocol unnegotiated or unknown\n");
break;
}
lReturn = SCardTransmit(hCardHandle,SCARD_PCI_T0,sendCommand,7,
NULL,pbRecv,&dwRecv );
if ( SCARD_S_SUCCESS != lReturn )
{
printf("Failed SCardTransmit\n");
}
lReturn = SCardReleaseContext(hContext);
return 0;
}
答案 0 :(得分:0)
正如退休忍者的评论所示。变量dwRecv需要初始化,在初始化变量后程序工作。感谢。