Пример #1
0
        public bool Connect()
        {
            bool ret = false;

            try {
                if (isLinkedReader() == false)
                {
                    return(false);
                }

                uint protocol;
                var  result = SCardAPI.SCardConnect(this.context, this.LinkedReaderName, SCardAPI.SCARD_SHARE_SHARED, SCardAPI.SCARD_PROTOCOL_T1, out handle, out protocol);
                if (result != SCardResult.SCARD_S_SUCCESS)
                {
                    throw (new Exception("SCardConnect"));
                }

                this.recvBuff = new byte[1024];

                ret = true;
            } catch (Exception) {
                this.LinkedReaderName = "";
            }
            return(ret);
        }
Пример #2
0
        public byte[] GetATR()
        {
            var resultAtr = new List <byte>();

            try {
                uint   atrLen = 33;
                byte[] atr    = new byte[33];

                uint pcchReaderLen = 0;;
                uint pdwState      = 0;
                uint pdwProtocol   = 0;

                var result = SCardAPI.SCardStatus(this.handle,
                                                  null, ref pcchReaderLen,
                                                  out pdwState, out pdwProtocol,
                                                  atr, ref atrLen);

                if (result != SCardResult.SCARD_S_SUCCESS)
                {
                    return(null);
                }

                for (int index = 0; index < atrLen; index++)
                {
                    resultAtr.Add(atr[index]);
                }
            } catch (Exception) {
            }
            return(resultAtr.ToArray());
        }
Пример #3
0
 public void Dispose()
 {
     Disconnect();
     if (this.context != IntPtr.Zero)
     {
         SCardAPI.SCardReleaseContext(this.context);
         this.context          = IntPtr.Zero;
         this.LinkedReaderName = "";
     }
 }
Пример #4
0
        private bool create()
        {
            SCardResult result = SCardAPI.SCardEstablishContext(SCardAPI.SCARD_SCOPE_USER, IntPtr.Zero, IntPtr.Zero, out this.context);

            if (result != SCardResult.SCARD_S_SUCCESS)
            {
                this.context = IntPtr.Zero;
                return(false);
            }
            return(true);
        }
Пример #5
0
        public APDUresponse SendandResponse(byte[] apdu)
        {
            APDUresponse res = null;

            try {
                int recvSize = SCardAPI.SCardTransmit(this.handle, apdu, this.recvBuff);
                res = new APDUresponse(recvBuff, recvSize);
            } catch (Exception) {
            } finally {
                logResponse(apdu, res);
            }
            return(res);
        }
Пример #6
0
 protected bool Disconnect()
 {
     if (this.handle != IntPtr.Zero)
     {
         var result = SCardAPI.SCardDisconnect(this.handle, SCardAPI.SCARD_LEAVE_CARD);
         if (result == SCardResult.SCARD_S_SUCCESS)
         {
             this.handle           = IntPtr.Zero;
             this.recvBuff         = null;
             this.LinkedReaderName = "";
             return(true);
         }
     }
     return(false);
 }
Пример #7
0
        public bool isLinkedReader()
        {
            bool ret = false;

            try {
                if (this.context == IntPtr.Zero)
                {
                    return(false);
                }

                // get size
                uint readerSize = 0;
                var  result     = SCardAPI.SCardListReaders(this.context, null, null, ref readerSize);
                if (result != SCardResult.SCARD_S_SUCCESS)
                {
                    throw (new Exception("SCardListReaders"));
                }

                // get readerData
                char[] readerData = new char[readerSize];
                result = SCardAPI.SCardListReaders(this.context, null, readerData, ref readerSize);
                if (result != SCardResult.SCARD_S_SUCCESS)
                {
                    throw (new Exception("SCardListReaders"));
                }

                // リーダー・ライターの名称分割(\u0000で区切られている)
                string[] readers = getNames(readerData);
                if (readers.Count() <= 0)
                {
                    throw (new Exception("getNames"));
                }

                // select target
                bool find = false;
                {
                    if (targetReaderNames.Count <= 0)
                    {
                        this.LinkedReaderName = readers[0];
                        find = true;
                    }
                    else
                    {
                        foreach (string readerName in readers)
                        {
                            foreach (string target in targetReaderNames)
                            {
                                if (readerName.StartsWith(target, StringComparison.OrdinalIgnoreCase))
                                {
                                    this.LinkedReaderName = readerName;
                                    find = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                ret = find;
            } catch (Exception) {
            }
            return(ret);
        }