예제 #1
0
        public bool ConnectReader(string SelectedReader)
        {
            try
            {
                // Connect to selected reader
                LastError = WinSCard.SCardConnect(
                    _context,
                    SelectedReader,
                    _shareMode,
                    _protocol,
                    ref _cardHandle,
                    ref activeProtocol);

                if (!IsSuccess(LastError))
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #2
0
        public bool ResetReader(
            string SelectedReader,
            out string ATR)
        {
            String atr_temp = "";
            String s        = "";
            bool   boRet    = true;

            ATR = atr_temp;

            // Disconnect to with reset disposition
            LastError = WinSCard.SCardDisconnect(
                _cardHandle,
                (int)SCardReaderDisposition.Reset);

            if (!IsSuccess(LastError))
            {
                return(false);
            }

            // Connect to selected reader
            if (!ConnectReader(SelectedReader))
            {
                return(false);
            }

            // Init readerState
            SCARD_READERSTATE readerState = new SCARD_READERSTATE
            {
                RdrName       = SelectedReader,
                RdrCurrState  = 0,
                RdrEventState = 0,
                UserData      = "Card",
            };

            // Get Status Change
            LastError = WinSCard.SCardGetStatusChange(
                _context,
                0,
                ref readerState,
                1);

            if (!IsSuccess(LastError))
            {
                return(false);
            }

            StringBuilder hex = new StringBuilder(readerState.ATRValue.Length * 2);

            foreach (byte b in readerState.ATRValue)
            {
                hex.AppendFormat("{0:X2}", b);
            }
            atr_temp = hex.ToString();

            ATR = atr_temp;
            return(boRet);
        }
예제 #3
0
        public bool ReleaseContext()
        {
            try
            {
                LastError = WinSCard.SCardReleaseContext(
                    _context);

                if (!IsSuccess(LastError))
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #4
0
        public bool DisconnectReader()
        {
            try
            {
                // Disconnect to selected Card Handle
                LastError = WinSCard.SCardDisconnect(
                    _cardHandle,
                    (int)SCardReaderDisposition.Unpower);

                if (!IsSuccess(LastError))
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #5
0
        public bool EstablishContext()
        {
            try
            {
                LastError = WinSCard.SCardEstablishContext(
                    _scope,
                    IntPtr.Zero,
                    IntPtr.Zero,
                    out _context);

                if (!IsSuccess(LastError))
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #6
0
        public string SendAPDU(string sAPDU)
        {
            SCARD_IO_REQUEST sendRequest;

            sendRequest.dwProtocol  = (int)activeProtocol;
            sendRequest.cbPciLength = 8;

            SCARD_IO_REQUEST receiveRequest;

            receiveRequest.cbPciLength = 8;
            receiveRequest.dwProtocol  = (int)activeProtocol;

            byte[] abAPDU   = new byte[300];
            byte[] abResp   = new byte[300];
            UInt32 wLenSend = 0;
            Int32  wLenRecv = 260;

            sAPDU    = Utility.RemoveNonHexa(sAPDU);
            wLenSend = Utility.StrByteArrayToByteArray(sAPDU, ref abAPDU);

            LastError = WinSCard.SCardTransmit(
                _cardHandle,
                ref sendRequest,
                abAPDU,
                (int)wLenSend,
                ref receiveRequest,
                abResp,
                ref wLenRecv);

            if (!IsSuccess(LastError))
            {
                return(String.Empty);
            }

            return(Utility.ByteArrayToStrByteArray(
                       abResp,
                       (UInt16)wLenRecv));
        }
예제 #7
0
        public List <string> GetReaderList()
        {
            List <string> retval      = new List <string>();
            uint          pcchReaders = 0;
            int           nullindex   = -1;
            char          nullchar    = (char)0;

            try
            {
                // Establish context
                if (!EstablishContext())
                {
                    return(null);
                }

                // Find out length of the available readers character
                LastError = WinSCard.SCardListReaders(
                    _context,
                    null,
                    null,
                    ref pcchReaders);

                if (!IsSuccess(LastError))
                {
                    return(null);
                }

                // Create a buffer and fill it
                byte[] szBuffer = new byte[pcchReaders];
                LastError = WinSCard.SCardListReaders(
                    _context,
                    null,
                    szBuffer,
                    ref pcchReaders);

                if (!IsSuccess(LastError))
                {
                    return(null);
                }

                // Convert szReaders to string
                string sBuffer = Encoding.ASCII.GetString(szBuffer);

                // Convert length of the available readers character to int
                int len = (int)pcchReaders;

                while (sBuffer[0] != (char)0)
                {
                    nullindex = sBuffer.IndexOf(nullchar);
                    string reader = sBuffer.Substring(0, nullindex);

                    retval.Add(reader);

                    len     = len - (reader.Length + 1);
                    sBuffer = sBuffer.Substring(nullindex + 1, len);
                }

                // Release context
                ReleaseContext();

                return(retval);
            }
            catch (Exception)
            {
                throw;
            }
        }