Beispiel #1
0
        protected virtual void UpdateState()
        {
            uint rc;

            _reader_state = SCARD.STATE_UNAWARE;
            _card_atr     = null;

            if (Connected)
            {
                byte[] atr_buffer = new byte[36];
                uint   atr_length = 36;

                uint dummy = 0;

                rc =
                    SCARD.Status(_hCard, IntPtr.Zero, ref dummy,
                                 ref _reader_state, ref _active_protocol, atr_buffer,
                                 ref atr_length);
                if (rc != SCARD.S_SUCCESS)
                {
                    _last_error = rc;
                    return;
                }

                _card_atr = new CardBuffer(atr_buffer, (int)atr_length);
            }
            else
            {
                SCARD.READERSTATE[] states = new SCARD.READERSTATE[1];

                states[0]                = new SCARD.READERSTATE();
                states[0].szReader       = _reader_name;
                states[0].pvUserData     = IntPtr.Zero;
                states[0].dwCurrentState = 0;
                states[0].dwEventState   = 0;
                states[0].cbAtr          = 0;
                states[0].rgbAtr         = null;

                try
                {
                    rc = SCARD.GetStatusChange(_hContext, 0, states, 1);
                }
                catch (ThreadInterruptedException)
                {
                    rc = SCARD.E_CANCELLED;
                }

                if (rc != SCARD.S_SUCCESS)
                {
                    _last_error = rc;
                    return;
                }

                _reader_state = states[0].dwEventState;

                if ((_reader_state & SCARD.STATE_PRESENT) != 0)
                {
                    _card_atr = new CardBuffer(states[0].rgbAtr, (int)states[0].cbAtr);
                }
            }
        }