Esempio n. 1
0
        public static bool CardPresent(string readerName)
        {
            int    retCode;
            IntPtr hContext = new IntPtr();

            retCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext);
            if (retCode != PcscProvider.SCARD_S_SUCCESS)
            {
                throw new Exception("Unable to establish context", new Exception(PcscProvider.GetScardErrMsg(retCode)));
            }

            PcscProvider.SCARD_READERSTATE state = new PcscProvider.SCARD_READERSTATE();
            state.szReader = readerName;

            retCode = PcscProvider.SCardGetStatusChange(hContext, 3000, ref state, 1);
            if (retCode != 0)
            {
                throw new Exception("Unable to get status", new Exception(PcscProvider.GetScardErrMsg(retCode)));
            }
            else
            {
                //state.dwCurrentState >>= 32;
                if (((Int32)(state.dwEventState) & PcscProvider.SCARD_STATE_PRESENT) == PcscProvider.SCARD_STATE_PRESENT)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Esempio n. 2
0
        public static byte[] getAtr(string readerName)
        {
            byte[] atr;
            int    retCode;
            IntPtr hContext = new IntPtr();

            try
            {
                PcscProvider.SCARD_READERSTATE state = new PcscProvider.SCARD_READERSTATE();
                state.szReader = readerName;

                retCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext);
                if (retCode != PcscProvider.SCARD_S_SUCCESS)
                {
                    return(null);
                }

                retCode = PcscProvider.SCardGetStatusChange(hContext, 3000, ref state, 1);
                if (retCode != 0)
                {
                    atr = null;
                }
                else
                {
                    if ((state.dwEventState & PcscProvider.SCARD_STATE_PRESENT) == PcscProvider.SCARD_STATE_PRESENT)
                    {
                        atr = state.rgbAtr.Take(state.cbAtr).ToArray();
                    }
                    else
                    {
                        atr = null;
                    }
                }

                PcscProvider.SCardReleaseContext(hContext);

                return(atr);
            }
            catch (Exception)
            {
                return(null);
            }
        }