Пример #1
0
        private byte[] GetAnswerToReset()
        {
            int result = 0;

            byte[] readerName = null;
            uint   readerLen  = 0;
            uint   state      = 0;
            uint   protocol   = 0;

            byte[] atr    = null;
            uint   atrLen = 0;

            result = WinConCard.SCardStatus(card, readerName, ref readerLen, out state, out protocol, atr, ref atrLen);

            if (result != WinConCard.SCARD_S_SUCCESS)
            {
                throw new CardDExcep(result);
            }

            readerName = new byte[readerLen];
            atr        = new byte[atrLen];
            result     = WinConCard.SCardStatus(card, readerName, ref readerLen, out state, out protocol, atr, ref atrLen);

            if (result != WinConCard.SCARD_S_SUCCESS)
            {
                throw new CardDExcep(result);
            }

            #region Debug output
#if DEBUG
            if (outputDebugString)
            {
                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < atrLen; i++)
                {
                    sb.AppendFormat("{0:X2}", atr[i]);
                }

                Debug.WriteLine(sb.ToString());
            }
#endif
            #endregion

            ASCIICon msReaderName = new ASCIICon(readerName);

            if (result != WinConCard.SCARD_S_SUCCESS)
            {
                throw new CardDExcep(result);
            }

            return(atr);
        }
Пример #2
0
        public CardReader()
        {
            int result = 0;

            result = WinConCard.SCardEstablishContext(WinConCard.Scope.SCARD_SCOCPE_SYSTEM, IntPtr.Zero, IntPtr.Zero, out context);

            if (result != WinConCard.SCARD_S_SUCCESS)
            {
                Debug.WriteLine(WinConCard.SCardErrorMessage(result));
            }

            byte[] readers     = null;
            uint   readerCount = 0;

            result = WinConCard.SCardListReaders(context, null, readers, ref readerCount);

            readers          = new byte[readerCount];
            result           = WinConCard.SCardListReaders(context, null, readers, ref readerCount);
            availableReaders = new ASCIICon(readers);

            if (result != WinConCard.SCARD_S_SUCCESS)
            {
                Debug.WriteLine(WinConCard.SCardErrorMessage(result));
            }

            //Start a background worker thread which monitors available card readers.
            if ((availableReaders.Count > 0))
            {
                readerStates = new WinConCard.SCARD_READERSTATE[availableReaders.Count];

                for (int i = 0; i < readerStates.Length; i++)
                {
                    readerStates[i].szReader = availableReaders.ToArray()[i];
                }

                monitorThread = new BackgroundWorker();
                monitorThread.WorkerSupportsCancellation = true;
                monitorThread.DoWork += WaitChangeStatus;
                monitorThread.RunWorkerAsync();
            }
        }
Пример #3
0
        public CardReader()
        {
            int result = 0;

            result = WinConCard.SCardEstablishContext(WinConCard.Scope.SCARD_SCOCPE_SYSTEM, IntPtr.Zero, IntPtr.Zero, out context);

            if (result != WinConCard.SCARD_S_SUCCESS)
            {
                Debug.WriteLine(WinConCard.SCardErrorMessage(result));
            }

            byte[] readers = null;
            uint readerCount = 0;
            result = WinConCard.SCardListReaders(context, null, readers, ref readerCount);

            readers = new byte[readerCount];
            result = WinConCard.SCardListReaders(context, null, readers, ref readerCount);
            availableReaders = new ASCIICon(readers);

            if (result != WinConCard.SCARD_S_SUCCESS)
            {
                Debug.WriteLine(WinConCard.SCardErrorMessage(result));
            }

            //Start a background worker thread which monitors available card readers.
            if ((availableReaders.Count > 0))
            {
                readerStates = new WinConCard.SCARD_READERSTATE[availableReaders.Count];

                for (int i = 0; i < readerStates.Length; i++)
                {
                    readerStates[i].szReader = availableReaders.ToArray()[i];
                }

                monitorThread = new BackgroundWorker();
                monitorThread.WorkerSupportsCancellation = true;
                monitorThread.DoWork += WaitChangeStatus;
                monitorThread.RunWorkerAsync();
            }
        }