SCardTrasmit返回空接收字节数组

时间:2013-05-21 12:04:59

标签: c# nfc pcsc

我正在使用ACS阅读器和NFC标签进行c#PC / SC实施。

我已将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。

现在,运行此代码我得到一个成功的返回代码,我收到一致的接收字节长度值,但相应的字节数组为空(零)。

任何人都可以就此提出建议吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

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,该参数使用属性InOut声明。