Edoardo
Edoardo

Reputation: 95

SCardTrasmit return empty receive byte array

I'm working on a c# PC/SC implementation with ACS readers and NFC Tags.

I have prototyped the SCardTransmit as follows:

[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);

And I have the following code:

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

All the (not shown) init'ing stuff seems to works fine and I can succesfully get the tag's ATR.

Now, running this code I get a success return code, I receive a consistent received bytes lenght value but the corrisponding byte array is empty (zeroes).

Anyone can advice on this?

Thank you.

Upvotes: 0

Views: 709

Answers (1)

Codo
Codo

Reputation: 78915

The prototype for SCardTransmit is most likely the problem. I would write it as:

[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);

The main difference is the second to last parameter pbRecvBuffer, which is declared with the attributes In and Out.

Upvotes: 1

Related Questions