/// <summary> /// Sends an APDU to the smartcard. /// </summary> /// <param name="apdu">The apdu to send to the smartcard.</param> /// <returns>An ApduReply instance, representing the reply from the smartcard.</returns> /// <exception cref="ObjectDisposedException">The SmartcardReader has been closed.</exception> /// <exception cref="ArgumentNullException"><i>apdu</i> is a null reference.</exception> /// <exception cref="SmartcardException">An error occurred while communication with the smartcard reader.</exception> public ApduReply SendCommand(ApduCommand apdu) { if (m_Disposed) throw new ObjectDisposedException(this.GetType().FullName); if (apdu == null) throw new ArgumentNullException("apdu", ResourceController.GetString("Error_ParamNull")); if (m_Card == IntPtr.Zero) throw new SmartcardException(ResourceController.GetString("Error_SmartcardNotConnected")); SCARD_IO_REQUEST sendPci = new SCARD_IO_REQUEST(m_ActiveProtocol, 8); byte[] buffer = new byte[1024]; int recvSize = buffer.Length; // send the message to the smart card int ret = NativeMethods.SCardTransmit(m_Card, ref sendPci, apdu.InternalBytes, apdu.InternalLength, IntPtr.Zero, buffer, ref recvSize); if (ret != NativeMethods.SCARD_S_SUCCESS || recvSize < 2) throw new SmartcardException(ResourceController.GetString("Error_SmartcardTransmit"), ret); byte[] result = new byte[recvSize - 2]; Array.Copy(buffer, 0, result, 0, result.Length); byte[] status = new byte[2]; Array.Copy(buffer, recvSize - status.Length, status, 0, status.Length); return new ApduReply(status, result); }
internal static extern int SCardTransmit(IntPtr hCard, ref SCARD_IO_REQUEST pioSendPci, byte[] pbSendBuffer, int cbSendLength, IntPtr pioRecvPci, byte[] pbRecvBuffer, ref int pcbRecvLength);
internal static extern int SCardTransmit(IntPtr hCard, ref SCARD_IO_REQUEST pioSendPci, byte[] pbSendBuffer, int cbSendLength, IntPtr pioRecvPci, byte[] pbRecvBuffer, ref int pcbRecvLength);