예제 #1
0
        internal bool Connect()
        {
            bool ret = false;

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

                uint protocol;
                var  result = SCardAPI.SCardConnect(this.context, this.targetReaderName, 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 ex) {
                logger.Error(ex);
            }
            return(ret);
        }
예제 #2
0
        private bool isLinkedDevice()
        {
            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 (string.IsNullOrEmpty(targetReaderName))
                    {
                        this.targetReaderName = readers[0];
                        find = true;
                    }
                    else
                    {
                        foreach (string readerName in readers)
                        {
                            if (readerName.StartsWith(this.targetReaderName, StringComparison.OrdinalIgnoreCase))
                            {
                                find = true;
                                break;
                            }
                        }
                    }
                }
                ret = find;
            } catch (Exception ex) {
                logger.Error(ex);
            }
            return(ret);
        }
예제 #3
0
 public void Dispose()
 {
     Disconnect();
     if (this.context != IntPtr.Zero)
     {
         SCardAPI.SCardReleaseContext(this.context);
         this.context = IntPtr.Zero;
     }
 }
예제 #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)
            {
                logger.Error("SCardEstablishContext");
                this.context = IntPtr.Zero;
                return(false);
            }
            return(true);
        }
예제 #5
0
        internal APDUresponse SendandResponse(byte[] apdu)
        {
            APDUresponse res = null;

            try {
                int recvSize = SCardAPI.SCardTransmit(this.handle, apdu, this.recvBuff);
                res = new APDUresponse(recvBuff, recvSize);
            } catch (Exception ex) {
                logger.Error(ex);
            } finally {
                logResponse(apdu, res);
            }
            return(res);
        }
예제 #6
0
 internal 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;
             return(true);
         }
     }
     return(false);
 }