Esempio n. 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 = WinSCard.SCardStatus(card, readerName, ref readerLen, out state, out protocol, atr, ref atrLen);

            if (result != WinSCard.SCARD_S_SUCCESS)
            {
                throw new PCSCException(result);
            }

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

            if (result != WinSCard.SCARD_S_SUCCESS)
            {
                throw new PCSCException(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

            MultiString msReaderName = new MultiString(readerName);

            if (result != WinSCard.SCARD_S_SUCCESS)
            {
                throw new PCSCException(result);
            }

            return(atr);
        }
Esempio n. 2
0
        public PCSCReader()
        {
            int result = 0;

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

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

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

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

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

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

            //Start a background worker thread which monitors available card readers.
            if ((availableReaders.Count > 0))
            {
                readerStates = new WinSCard.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();
            }
        }
Esempio n. 3
0
        public PCSCReader()
        {
            int result = 0;

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

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

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

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

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

            //Start a background worker thread which monitors available card readers.
            if ((availableReaders.Count > 0))
            {
                readerStates = new WinSCard.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();
            }
        }