private void UpdateReaderState(SCardCardReaderState state, string cardReaderName) { PcscCardReader CardReader = (PcscCardReader)this.Readers[cardReaderName]; SCardReaderState NewState = state.dwEventState; if ((NewState & SCardReaderState.Unavailable) != 0 && this.scardApi.ListReaders().Contains(cardReaderName) == false) { //CardReader was removed, if state changes to unavailiable _and_ reader is not in the list anymore. //Otherwise, reader was opened in direct mode this.RemoveCardReader(CardReader); return; } else { try { // CardReader state changed CardReader.CardReaderState = state; return; } catch (Exception Error) { Trace.Fail($"Exception was thrown inside status change handler. This may lead to a blocking application! Origianl Exception was: {Error.Message}"); throw; } } }
private void InsertNewCardReaders() { string[] CardReaderNames = this.scardApi.ListReaders(); foreach (string CardReadername in CardReaderNames) { if (!this.Readers.ContainsName(CardReadername)) { PcscCardReader NewCardReader = this.CreateReader(CardReadername); this.AddCardReader(NewCardReader); } } }
protected internal override void ResetCard(Protocol protocol) { if (this.CardHandle != IntPtr.Zero) { this.scardApi.Reconnect(this.CardHandle, SCardDisposition.Reset, SCardShareMode.Exclusive, PcscCardReader.ProtocolToScardProtocol(protocol)); } }
protected internal override void ConnectCard(Protocol protocol) { if (this.CardHandle != IntPtr.Zero) { throw new InvalidOperationException("Smart Card already connected"); } try { this.CardHandle = this.scardApi.Connect(this.Name, SCardShareMode.Exclusive, PcscCardReader.ProtocolToScardProtocol(protocol)); } catch (SCardException Exception) { switch (Exception.Error) { case SCardError.RequestNotSupported: case SCardError.ProtocolMismatch: throw new ProtocolNotSupportedException(this.SmartCard, protocol); case SCardError.SharingViolation: throw new SmartCardInUseException(this.SmartCard); default: throw; } } }