我正在研究一个c# PC/SC实现与ACS阅读器和NFC标签。
我已经制作了SCardTransmit的原型如下:
[DllImport("winscard.dll")]
public static extern int SCardTransmit (Int32 hCard, ref SCARD_IO_REQUEST pioSendRequest, byte[] SendBuff, Int32 SendBuffLen, ref SCARD_IO_REQUEST pioRecvRequest, byte[] RecvBuff, out Int32 RecvBuffLen);
我有以下代码:
SCard.SCARD_IO_REQUEST ioRequest = new SCard.SCARD_IO_REQUEST();
ioRequest.dwProtocol = Protocol; // Is going to be T=1
ioRequest.cbPciLength = 8;
byte[] cmdBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 };
byte[] rcvBytes = new byte[10];
int rcvLenght = 0;
retCode = SCard.SCardTransmit(Handle,
ref ioRequest, cmdBytes, cmdBytes.Length,
ref ioRequest, rcvBytes, out rcvLenght);
if (retCode != SCard.SCARD_S_SUCCESS)
throw new Exception("Failed querying tag UID: " + retCode);
所有(未显示)初始化的东西似乎工作正常,我可以成功地获得标签的ATR。
现在,运行这段代码,我得到一个成功的返回代码,我收到一个一致的接收字节长度值,但相应的字节数组是空的(0)。
有谁能给点建议吗?
谢谢。
SCardTransmit
的原型最有可能是问题所在。我将把它写成:
[DllImport("winscard.dll")]
public static extern uint SCardTransmit(Int32 hCard, SCARD_IO_REQUEST pioSendPci, [In] byte[] pbSendBuffer, uint cbSendLength, SCARD_IO_REQUEST pioRecvPci, [In, Out] byte[] pbRecvBuffer, ref uint pcbRecvLength);
主要区别在于倒数第二个参数pbRecvBuffer
,它用属性In
和Out
声明。