/// <summary> /// Sends a service request to the smart card and expects to receive data back from the card. /// </summary> /// <param name="apdu">The SmartCard ADPU command and response object.</param> /// <returns>adpu</returns> public SCardAdpu Transmit(SCardAdpu apdu) { if (_hContext == IntPtr.Zero) { throw new SCardException("No card reader context established. Must call SCard.EstablishContext() first."); } if (_hCard == IntPtr.Zero) { throw new SCardException("Not connected to card. Must call SCard.Connect() method first."); } byte[] send = apdu.GetSendBuffer(); byte[] recv = new byte[apdu.GetReceiveBufferSize()]; int recvLen = recv.Length; // this is an in/out value SCARD_IO_REQUEST ioRequest = new SCARD_IO_REQUEST(); ioRequest.dwProtocol = (uint)_protocol; ioRequest.cbPciLength = 8; IntPtr pioRecvPci = IntPtr.Zero; Debug.WriteLine("SCardTransmit-ADPU-send: " + ArrayUtils.HexEncode(send)); int rtn = SCardDll.SCardTransmit(_hCard, ref ioRequest, ref send[0], send.Length, pioRecvPci, ref recv[0], ref recvLen); EvalReturnCode(rtn); // resize the array if the bytes received are less than the buffer size if (recvLen < recv.Length) { Array.Resize <byte>(ref recv, (int)recvLen); } Debug.WriteLine("SCardTransmit-ADPU-recv: " + ArrayUtils.HexEncode(recv)); // update the apdu object with the receive data apdu.SetReceiveData(recv); return(apdu); }
/// <summary> /// Sends a service request to the smart card and expects to receive data back from the card. /// </summary> /// <param name="apdu">The SmartCard ADPU command and response object.</param> /// <returns>adpu</returns> public SCardAdpu Transmit(SCardAdpu apdu) { if (_hContext == IntPtr.Zero) throw new SCardException("No card reader context established. Must call SCard.EstablishContext() first."); if (_hCard == IntPtr.Zero) throw new SCardException("Not connected to card. Must call SCard.Connect() method first."); byte[] send = apdu.GetSendBuffer(); byte[] recv = new byte[apdu.GetReceiveBufferSize()]; int recvLen = recv.Length; // this is an in/out value SCARD_IO_REQUEST ioRequest = new SCARD_IO_REQUEST(); ioRequest.dwProtocol = (uint)_protocol; ioRequest.cbPciLength = 8; IntPtr pioRecvPci = IntPtr.Zero; Debug.WriteLine("SCardTransmit-ADPU-send: " + ArrayUtils.HexEncode(send)); int rtn = SCardDll.SCardTransmit(_hCard, ref ioRequest, ref send[0], send.Length, pioRecvPci, ref recv[0], ref recvLen); EvalReturnCode(rtn); // resize the array if the bytes received are less than the buffer size if (recvLen < recv.Length) Array.Resize<byte>(ref recv, (int)recvLen); Debug.WriteLine("SCardTransmit-ADPU-recv: " + ArrayUtils.HexEncode(recv)); // update the apdu object with the receive data apdu.SetReceiveData(recv); return apdu; }