Example #1
0
        /// <summary>
        /// カードコマンド送受信
        /// </summary>
        /// <param name="card">カードコンテキスト</param>
        /// <param name="sendPci">GetPciを使用して取得したPCI構造体</param>
        /// <param name="sendBuffer">送信データ</param>
        /// <param name="sendLen">送信データ長</param>
        /// <param name="recvPci">レスポンス格納用PCI構造体</param>
        /// <param name="recvBuffer">レスポンス格納バッファ</param>
        /// <param name="recvLen">[OUT]レスポンス長</param>
        /// <returns></returns>
        public static bool Transmit(IntPtr card, SCARD_IO_REQUEST sendPci,
                                    byte[] sendBuffer, int sendLen, ref SCARD_IO_REQUEST recvPci, byte[] recvBuffer, ref uint recvLen)
        {
            Debug.WriteLine("WinSCard.Transmit Send:" + Utility.ByteToHex(sendBuffer, 0, (int)sendLen));
            lastError = (WinSCardError)SCardTransmit(
                card, ref sendPci, sendBuffer, (uint)sendLen, ref recvPci, recvBuffer, ref recvLen);

            if (lastError == WinSCardError.SCARD_S_SUCCESS && recvBuffer[recvLen - 2] == 0x90 && recvBuffer[recvLen - 1] == 0x00)
            {
                Array.Copy(recvBuffer, recvLen - 2, lastRes, 0, lastRes.Length);
                Debug.WriteLine("WinSCard.Transmit OK:" + Utility.ByteToHex(recvBuffer, 0, (int)recvLen));
                return(true);
            }
            else if (lastError == WinSCardError.SCARD_S_SUCCESS)
            {
                Array.Copy(recvBuffer, recvLen - 2, lastRes, 0, lastRes.Length);
                Debug.WriteLine("WinSCard.Transmit Error:" + Utility.ByteToHex(recvBuffer, 0, (int)recvLen));
                return(false);
            }
            else
            {
                Debug.WriteLine("WinSCard.Transmit Error:" + lastError.ToString());
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// 書き込み時に指定する、レスポンス格納用構造体を取得
        /// </summary>
        /// <returns></returns>
        public static SCARD_IO_REQUEST GetDefPci()
        {
            SCARD_IO_REQUEST resPci = new SCARD_IO_REQUEST();

            resPci.dwProtocol  = WinSCard.SCARD_PROTOCOL_UNDEFINED;
            resPci.cbPciLength = (uint)Marshal.SizeOf(resPci);
            return(resPci);
        }
Example #3
0
 private static extern uint SCardTransmit(
     IntPtr hCard,
     ref SCARD_IO_REQUEST pioSendPci,
     byte[] pbSendBuffer,
     uint cbSendLength,
     ref SCARD_IO_REQUEST pioRecvPci,
     byte[] pbRecvBuffer,
     ref uint pcbRecvLength
     );
Example #4
0
        private void PcscCommand(byte[] data, int len, out uint recvLen)
        {
            SCARD_IO_REQUEST reqCpi = WinSCard.GetPci(protocol);
            SCARD_IO_REQUEST resCpi = WinSCard.GetDefPci();

            recvLen = (uint)res.Length;
            if (!WinSCard.Transmit(cardContext, reqCpi, data, len, ref resCpi, res, ref recvLen))
            {
                throw GetException();
            }

            if (res[recvLen - 2] != 0x90 || res[recvLen - 1] != 0x00)
            {
                throw new PcscException(WinSCardError.SCARD_F_UNKNOWN_ERROR, new byte[] { res[recvLen - 2], res[recvLen - 1] });
            }
        }
Example #5
0
        static WinSCard()
        {
            SCARD_PCI_UNDEFINED             = new SCARD_IO_REQUEST();
            SCARD_PCI_UNDEFINED.dwProtocol  = SCARD_PROTOCOL_UNDEFINED;
            SCARD_PCI_UNDEFINED.cbPciLength = (uint)Marshal.SizeOf(SCARD_PCI_UNDEFINED);

            SCARD_PCI_T0             = new SCARD_IO_REQUEST();
            SCARD_PCI_T0.dwProtocol  = SCARD_PROTOCOL_T0;
            SCARD_PCI_T0.cbPciLength = (uint)Marshal.SizeOf(SCARD_PCI_T0);

            SCARD_PCI_T1             = new SCARD_IO_REQUEST();
            SCARD_PCI_T1.dwProtocol  = SCARD_PROTOCOL_T1;
            SCARD_PCI_T1.cbPciLength = (uint)Marshal.SizeOf(SCARD_PCI_T1);

            SCARD_PCI_RAW             = new SCARD_IO_REQUEST();
            SCARD_PCI_RAW.dwProtocol  = SCARD_PROTOCOL_RAW;
            SCARD_PCI_RAW.cbPciLength = (uint)Marshal.SizeOf(SCARD_PCI_RAW);
            lastRes = new byte[2];
        }