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; } } }
public void CardChangeEventCntSetEventStateValueTest() { var state = new SCardReaderState { EventStateValue = IntPtr.Zero }; state.CardChangeEventCnt .Should() .Be(0); //set the counter (upper 2 bytes of the event state) to 1 state.EventStateValue = unchecked (new IntPtr(0x00010000)); state.CardChangeEventCnt .Should() .Be(1); //Set the maximum value //The maximum value depends on the architecture. //If the process is running 32-bit, IntPtr(long) throws an exception. if (IntPtr.Size == sizeof(Int64)) { state.EventStateValue = unchecked (new IntPtr(0xFFFF0000)); state.CardChangeEventCnt .Should() .Be(0xFFFF); } else { state.EventStateValue = unchecked (new IntPtr(0x7FFF0000)); state.CardChangeEventCnt .Should() .Be(0x7FFF); } }
private void RefreshData() { card = context.GetReaderStatus(reader); string atr = BitConverter.ToString(card.Atr ?? new byte[0]); if (atr == "") { statusLabel.ForeColor = Color.Orange; statusLabel.Text = "ОЖИДАНИЕ КАРТЫ"; atrTxtBox.Text = "ВСТАВЬТЕ КАРТУ"; } else { if (atrs.Contains(atr)) { statusLabel.ForeColor = Color.Green; statusLabel.Text = "АВТОРИЗОВАНО"; } else { statusLabel.ForeColor = Color.FromArgb(220, 10, 10); statusLabel.Text = "НЕ АВТОРИЗОВАНО"; } atrTxtBox.Text = atr; } }
private void ResetCardReaderStates() { this.pnpNotificationDeviceState = SCardReaderState.Unaware; foreach (CardReaderBase CardReader in this.owner.Readers.ToArray()) { this.owner.RemoveCardReader(CardReader); } this.InitialSetupCardReaders(); }
string Readcardid() { SCardContext card = new SCardContext(); card.Establish(SCardScope.System); string[] readers = card.GetReaders(); SCardReaderState status = card.GetReaderStatus(readers[0]); return(BitConverter.ToString(status.Atr ?? new byte[0])); }
private void HandleCardReaderStateChange(string cardReaderName, SCardCardReaderState state) { if (cardReaderName == PollThread.PnpNotificationDeviceName) { this.pnpNotificationDeviceState = state.dwEventState; this.owner.InsertNewCardReaders(); } else { this.owner.UpdateReaderState(state, cardReaderName); return; } }
public void RefreshCardData() { SCardContext card = new SCardContext(); card.Establish(SCardScope.System); string[] readers = card.GetReaders(); SCardReaderState status = card.GetReaderStatus(readers[0]); CardDataBox.Text += status.UserData + " - User Data\n" + Environment.NewLine; CardDataBox.Text += status.ReaderName + " - Reader\n" + Environment.NewLine; CardDataBox.Text += status.CurrentStateValue + " - Curent State Value\n" + Environment.NewLine; CardDataBox.Text += BitConverter.ToString(status.Atr ?? new byte[0]) + " - Answer to Reset\n" + Environment.NewLine; }
private static void PrintReaderState(SCardReaderState state) { Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); var atr = BitConverter.ToString(state.Atr ?? new byte[0]); Console.WriteLine($"Reader: {state.ReaderName}\n" + $"CurrentState: {state.CurrentState}\n" + $"EventState: {state.EventState}\n" + $"CurrentStateValue: {state.CurrentStateValue}\n" + $"EventStateValue: {state.EventStateValue}\n" + $"UserData: {state.UserData}\n" + $"CardChangeEventCnt: {state.CardChangeEventCnt}\n" + $"ATR: {atr}"); }
public void CardChangeEventCntSetTest() { var state = new SCardReaderState { EventState = SCRState.Empty, CardChangeEventCnt = 0 }; state.CardChangeEventCnt .Should() .Be(0); ((uint)state.EventStateValue.ToInt32()) .Should() .Be((uint)SCRState.Empty); state.EventState .Should() .Be(SCRState.Empty, "EventState should not change by setting the Counter"); //set the counter to 1 state.CardChangeEventCnt = 1; state.CardChangeEventCnt .Should() .Be(1); ((uint)state.EventStateValue.ToInt32()) .Should() .Be(0x00010010); state.EventState .Should() .Be(SCRState.Empty, "EventState should not change by setting the Counter"); //Set the maximum value state.CardChangeEventCnt = 65535; state.CardChangeEventCnt .Should() .Be(65535); ((uint)state.EventStateValue.ToInt32()) .Should().Be(0xFFFF0010); //try to set a value that is too high ((Action)(() => state.CardChangeEventCnt = 65536)) .ShouldThrow <ArgumentException>(); //try to set a negative value ((Action)(() => state.CardChangeEventCnt = -1)) .ShouldThrow <ArgumentException>(); }
public void CardChangeEventCntSetTest() { var state = new SCardReaderState { EventState = SCRState.Empty, CardChangeEventCnt = 0 }; state.CardChangeEventCnt .Should() .Be(0); ((uint) state.EventStateValue.ToInt32()) .Should() .Be((uint) SCRState.Empty); state.EventState .Should() .Be(SCRState.Empty, "EventState should not change by setting the Counter"); //set the counter to 1 state.CardChangeEventCnt = 1; state.CardChangeEventCnt .Should() .Be(1); ((uint) state.EventStateValue.ToInt32()) .Should() .Be(0x00010010); state.EventState .Should() .Be(SCRState.Empty, "EventState should not change by setting the Counter"); //Set the maximum value state.CardChangeEventCnt = 65535; state.CardChangeEventCnt .Should() .Be(65535); ((uint) state.EventStateValue.ToInt32()) .Should().Be(0xFFFF0010); //try to set a value that is too high ((Action) (() => state.CardChangeEventCnt = 65536)) .ShouldThrow<ArgumentException>(); //try to set a negative value ((Action) (() => state.CardChangeEventCnt = -1)) .ShouldThrow<ArgumentException>(); }
public SCardReaderState Status(out byte[] byteATR, out SCardReaderState eState, out SCardProtocolIdentifiers eProtocol) { if (this.m_fDisposed) { throw new ObjectDisposedException(base.GetType().FullName); } if (this.m_hCard == IntPtr.Zero) { throw new InvalidOperationException(); } StringBuilder builder1 = new StringBuilder(0x100); uint num3 = (uint)builder1.Capacity; byte[] atr = new byte[0x20]; uint num4 = (uint)atr.Length; uint dwState; uint dwProtocol; uint dwATRLength = (uint)atr.Length; uint num5 = WinSCard.SCardStatus(this.m_hCard, builder1, ref num3 /*builder1.Capacity*/, out dwState /*pdwState*/, out dwProtocol /*pdwProtocol*/, atr /*pbATR*/, ref dwATRLength /*pbcATRLen*/); if (num5 != 0) { //SmartCardException.RaiseWin32ResponseCode(num5); } eState = (SCardReaderState)dwState; eProtocol = (SCardProtocolIdentifiers)dwProtocol; if (dwATRLength > 0) { // copy only the valid portion of the ATR to the return buffer. byteATR = new byte[dwATRLength]; for (int i = 0; i < dwATRLength; i++) { byteATR[i] = atr[i]; } } else { byteATR = new byte[1]; byteATR[0] = 0;// return an invalid state. } return((SCardReaderState)dwState); }
public void CardChangeEventCntSetEventStateValueTest() { var state = new SCardReaderState { EventStateValue = IntPtr.Zero }; state.CardChangeEventCnt .Should() .Be(0); //set the counter (upper 2 bytes of the event state) to 1 state.EventStateValue = unchecked (new IntPtr(0x00010000)); state.CardChangeEventCnt .Should() .Be(1); //Set the maximum value state.EventStateValue = unchecked(new IntPtr(0xFFFF0000)); state.CardChangeEventCnt .Should() .Be(65535); }
public void CardChangeEventCntSetEventStateValueTest() { var state = new SCardReaderState { EventStateValue = IntPtr.Zero }; state.CardChangeEventCnt .Should() .Be(0); //set the counter (upper 2 bytes of the event state) to 1 state.EventStateValue = unchecked (new IntPtr(0x00010000)); state.CardChangeEventCnt .Should() .Be(1); //Set the maximum value state.EventStateValue = unchecked (new IntPtr(0xFFFF0000)); state.CardChangeEventCnt .Should() .Be(65535); }
public SCardError GetStatusChange( IntPtr hContext, IntPtr dwTimeout, SCardReaderState[] rgReaderStates) { SCARD_READERSTATE[] readerstates = null; int cReaders = 0; if (rgReaderStates != null) { cReaders = rgReaderStates.Length; readerstates = new SCARD_READERSTATE[cReaders]; for (int i = 0; i < cReaders; i++) readerstates[i] = rgReaderStates[i].pcsclite_rstate; } IntPtr retval = SCardGetStatusChange( (IntPtr)hContext, (IntPtr)dwTimeout, readerstates, (IntPtr)cReaders); SCardError rc = SCardHelper.ToSCardError(retval); if (rc == SCardError.Success) if (rgReaderStates != null) { for (int i = 0; i < cReaders; i++) /* replace with returned values */ rgReaderStates[i].pcsclite_rstate = readerstates[i]; } return rc; }
public SCardError GetStatusChange(IntPtr hContext, IntPtr dwTimeout, SCardReaderState[] rgReaderStates) { SCARD_READERSTATE[] readerstates = null; var cReaders = 0; if (rgReaderStates != null) { // copy the last known state into the buffer cReaders = rgReaderStates.Length; readerstates = new SCARD_READERSTATE[cReaders]; for (var i = 0; i < cReaders; i++) { readerstates[i] = rgReaderStates[i].UnixReaderState; } } var rc = SCardHelper.ToSCardError(SCardGetStatusChange( hContext, dwTimeout, readerstates, (IntPtr) cReaders)); if (rc != SCardError.Success || rgReaderStates == null) { return rc; } for (var i = 0; i < cReaders; i++) // replace with returned values rgReaderStates[i].UnixReaderState = readerstates[i]; return rc; }
public SCardError GetStatusChange(IntPtr hContext, IntPtr dwTimeout, SCardReaderState[] rgReaderStates) { SCARD_READERSTATE[] readerstates = null; var cReaders = 0; if (rgReaderStates != null) { // copy the last known state into the buffer cReaders = rgReaderStates.Length; readerstates = new SCARD_READERSTATE[cReaders]; for (var i = 0; i < cReaders; i++) { readerstates[i] = rgReaderStates[i].WindowsReaderState; } } // On a 64-bit platforms .ToInt32() will throw an OverflowException var timeout = unchecked((Int32) dwTimeout.ToInt64()); var rc = SCardHelper.ToSCardError( SCardGetStatusChange( hContext, timeout, readerstates, cReaders)); if (rc != SCardError.Success || rgReaderStates == null) { return rc; } for (var i = 0; i < cReaders; i++) { // replace with returned values rgReaderStates[i].WindowsReaderState = readerstates[i]; } return rc; }
public SCardError GetStatusChange( IntPtr hContext, IntPtr dwTimeout, SCardReaderState[] rgReaderStates) { SCARD_READERSTATE[] readerstates = null; int cReaders = 0; if (rgReaderStates != null) { cReaders = rgReaderStates.Length; readerstates = new SCARD_READERSTATE[cReaders]; for (int i = 0; i < cReaders; i++) readerstates[i] = rgReaderStates[i].winscard_rstate; } SCardError rc; // On a 64-bit platforms .ToInt32() will throw an OverflowException Int32 timeout = unchecked((Int32)dwTimeout.ToInt64()); rc = SCardHelper.ToSCardError( SCardGetStatusChange( (IntPtr)hContext, (Int32)timeout, readerstates, (Int32)cReaders)); if (rc == SCardError.Success) if (rgReaderStates != null) { for (int i = 0; i < cReaders; i++) /* replace with returned values */ rgReaderStates[i].winscard_rstate = readerstates[i]; } return rc; }
private void StartMonitor(Monitor monitor) { var readerStates = new SCardReaderState[monitor.ReaderNames.Length]; for (var i = 0; i < monitor.ReaderNames.Length; i++) { readerStates[i] = new SCardReaderState { ReaderName = monitor.ReaderNames[i], CurrentState = SCRState.Unaware }; } var rc = monitor.Context.GetStatusChange(IntPtr.Zero, readerStates); if (rc == SCardError.Success) { // initialize event for (var i = 0; i < readerStates.Length; i++) { var initState = readerStates[i].EventState & (~(SCRState.Changed)); OnInitialized(readerStates[i].Atr, monitor.ReaderNames[i], initState); monitor.PreviousStates[i] = initState; // remove "Changed" monitor.PreviousStateValues[i] = readerStates[i].EventStateValue; } while (!_isDisposed && !monitor.CancelRequested) { for (var i = 0; i < readerStates.Length; i++) { readerStates[i].CurrentStateValue = monitor.PreviousStateValues[i]; } // block until status change occurs rc = monitor.Context.GetStatusChange(monitor.Context.Infinite, readerStates); // Cancel? if (rc != SCardError.Success) { break; } for (var i = 0; i < readerStates.Length; i++) { var newState = readerStates[i].EventState; newState &= (~(SCRState.Changed)); // remove "Changed" var atr = readerStates[i].Atr; var previousState = monitor.PreviousStates[i]; var readerName = monitor.ReaderNames[i]; // Status change if (previousState != newState) { OnStatusChanged(atr, readerName, previousState, newState); } // Card inserted if (newState.CardIsPresent() && previousState.CardIsAbsent()) { OnCardInserted(atr, readerName, newState); } // Card removed if (newState.CardIsAbsent() && previousState.CardIsPresent()) { OnCardRemoved(atr, readerName, newState); } monitor.PreviousStates[i] = newState; monitor.PreviousStateValues[i] = readerStates[i].EventStateValue; } } } foreach (var state in readerStates) { state.Dispose(); } if (_isDisposed || monitor.CancelRequested || rc == SCardError.Cancelled) { return; } OnMonitorException(rc, "An error occured during SCardGetStatusChange(..)."); }