public bool StartSmartCardMonitor(CardInitializedEvent onCardInitialized, CardInsertedEvent onCardInserted, CardRemovedEvent onCardRemoved) { try { var contextFactory = ContextFactory.Instance; _sCardMonitor = new PCSC.SCardMonitor(contextFactory, SCardScope.System); if (onCardInitialized != null) { _sCardMonitor.Initialized += onCardInitialized; } if (onCardInserted != null) { _sCardMonitor.CardInserted += onCardInserted; } if (onCardRemoved != null) { _sCardMonitor.CardRemoved += onCardRemoved; } _sCardMonitor.Start(EnumDeviceNames.SmartCardContactlessReader); return(true); } catch (Exception ex) { Debug.WriteLine("StartCardMonitor Exception: " + ex.Message); return(false); } }
public void Connect() { Log.Debug("Connecting card reader"); try { CardContext = new SCardContext(); CardContext.Establish(SCardScope.System); CardMonitor = new SCardMonitor(CardContext); CardMonitor.CardInserted += CardInserted; CardMonitor.CardRemoved += CardRemoved; CardMonitor.Start(GetReader()); } catch (Exception) { Disconnect(); } if (CardContext == null || !CardContext.IsValid()) { Log.Warn("Card reader connection failed"); Disconnect(); } else { Log.Info("Card reader connected"); } }
public CardReader(CardRemovedEvent removedEvent, CardInsertedEvent insertedEvent) { TS.TraceI("Constructing CardReader object."); _systemCardContext = OpenSystemWideCardContext(); if (removedEvent != null || insertedEvent != null) { _monitor = new SCardMonitor(_systemCardContext); if (removedEvent != null) { TS.TraceV("Monitoring removedEvent"); _monitor.CardRemoved += new CardRemovedEvent(removedEvent); } if (insertedEvent != null) { TS.TraceV("Monitoring insertedEvent"); _monitor.CardInserted += new CardInsertedEvent(insertedEvent); } readerNames = GetReaderNames(); foreach (string s in readerNames) { TS.TraceV("Reader detected: \"{0}\".", s); } this.StartMonitor(); TS.TraceI("Monitor started."); } TS.TraceI("CardReader object constructed."); }
public void StopSmartCardMonitor() { if (_sCardMonitor != null) { _sCardMonitor.Cancel(); _sCardMonitor = null; } }
public MainWindow() { InitializeComponent(); setupLoggers(); menuRes = new MenuResourceDictionary(); System.Windows.Controls.Menu m = menuRes.getMenu(); cardIO = new SmartCardIO(); monitor = cardIO.getMonitor(); monitor.CardInserted += new CardInsertedEvent(CardEvent); monitor.CardRemoved += new CardRemovedEvent(CardEvent); monitor.Initialized += new CardInitializedEvent(CardEvent); monitor.StatusChanged += new StatusChangeEvent(StatusChanged); monitor.MonitorException += new MonitorExceptionEvent(MonitorException); monitor.Start(cardIO.getReaders()); }
static void Main(string[] args) { ConsoleKeyInfo keyinfo; Console.WriteLine("This program will monitor all SmartCard readers and display all status changes."); Console.WriteLine("Press a key to continue."); keyinfo = Console.ReadKey(); // Retrieve the names of all installed readers. SCardContext ctx = new SCardContext(); ctx.Establish(SCardScope.System); string[] readernames = ctx.GetReaders(); ctx.Release(); if (readernames == null || readernames.Length == 0) { Console.WriteLine("There are currently no readers installed."); return; } // Create a monitor object with its own PC/SC context. SCardMonitor monitor = new SCardMonitor( new SCardContext(), SCardScope.System); // Point the callback function(s) to the static defined methods below. monitor.CardInserted += new CardInsertedEvent(CardInserted); monitor.CardRemoved += new CardRemovedEvent(CardRemoved); monitor.Initialized += new CardInitializedEvent(Initialized); monitor.StatusChanged += new StatusChangeEvent(StatusChanged); monitor.MonitorException += new MonitorExceptionEvent(MonitorException); foreach (string reader in readernames) Console.WriteLine("Start monitoring for reader " + reader + "."); monitor.Start(readernames); // Let the program run until the user presses a key keyinfo = Console.ReadKey(); GC.KeepAlive(keyinfo); // Stop monitoring monitor.Cancel(); }
public static void Main() { Console.WriteLine("This program will monitor all SmartCard readers and display all status changes."); Console.WriteLine("Press a key to continue."); Console.ReadKey(); // Wait for user to press a key // Retrieve the names of all installed readers. string[] readerNames; using (var context = new SCardContext()) { context.Establish(SCardScope.System); readerNames = context.GetReaders(); context.Release(); } if (readerNames == null || readerNames.Length == 0) { Console.WriteLine("There are currently no readers installed."); return; } // Create a monitor object with its own PC/SC context. // The context will be released after monitor.Dispose() var monitor = new SCardMonitor(new SCardContext(), SCardScope.System); // Point the callback function(s) to the anonymous & static defined methods below. monitor.CardInserted += (sender, args) => DisplayEvent("CardInserted", args); monitor.CardRemoved += (sender, args) => DisplayEvent("CardRemoved", args); monitor.Initialized += (sender, args) => DisplayEvent("Initialized", args); monitor.StatusChanged += StatusChanged; monitor.MonitorException += MonitorException; foreach (var reader in readerNames) { Console.WriteLine("Start monitoring for reader " + reader + "."); } monitor.Start(readerNames); // Let the program run until the user presses a key Console.ReadKey(); // Stop monitoring monitor.Cancel(); // Dispose monitor resources (SCardContext) monitor.Dispose(); }
/// <summary> /// Tries to connect to the selected reader. /// </summary> /// <returns>Null if successful. The error message if not.</returns> public string StartMonitoringSelectedReader(string readerName) { if (string.IsNullOrEmpty(readerName)) return "Reader name is null or empty"; if (!_readers.Contains(readerName)) return "The reader does not exist. [Logic Error]"; _connectedReader = readerName; SCardContext context = new SCardContext(); context.Establish(SCardScope.System); SCardReader reader = new SCardReader(context); SCardError result = SCardError.InternalError; try { result = reader.Connect(readerName, SCardShareMode.Shared, SCardProtocol.Any); } catch (Exception) { context.Dispose(); reader.Dispose(); return SCardHelper.StringifyError(result); } _monitor = new SCardMonitor(new SCardContext(), SCardScope.System, true); _monitor.Initialized += (_cardInitalised); _monitor.CardInserted += (_cardInserted); _monitor.CardRemoved += (_cardRemoved); _monitor.Start(readerName); return null; }
// returns a new monitor each time. public SCardMonitor getMonitor() { SCardMonitor monitor = new SCardMonitor(ctx, scope); return monitor; }
/// <summary> /// Create UID reader context and wait for events. /// </summary> /// <exception cref="InvalidOperationException"> /// There are currently no readers installed. /// </exception> protected override void ExecuteContext() { // Retrieve the names of all installed readers. using( Context = new SCardContext() ) { Context.Establish( SCardScope.System ); string[] readernames = null; try { readernames = Context.GetReaders(); } catch( PCSCException ) {} finally { if( null == readernames || 0 == readernames.Length ) { Log.Error( "There are currently no readers installed." ); } } // Create a monitor object with its own PC/SC context. SCardMonitor monitor = new SCardMonitor( new SCardContext(), SCardScope.System ); // Point the callback function(s) to the static defined methods below. monitor.CardInserted += CardInserted; if( null != readernames ) { foreach( string reader in readernames ) { Log.InfoFormat( "Start monitoring for reader '{0}'.", reader ); } monitor.Start( readernames ); } // Wait for the parent application to signal us to exit. ExitApplication = new ManualResetEvent( false ); ExitApplication.WaitOne(); // Stop monitoring monitor.Cancel(); } }
/// <summary> /// Create UID reader context and wait for events. /// </summary> /// <exception cref="InvalidOperationException"> /// There are currently no readers installed. /// </exception> protected virtual void ExecuteContext() { try { // Retrieve the names of all installed readers. using( Context = new SCardContext() ) { Context.Establish( SCardScope.System ); string[] readernames = null; try { Log.Info( "Attempting to retrieve connected readers..." ); readernames = Context.GetReaders(); } catch( PCSCException ) {} SCardMonitor monitor = null; if( null == readernames || 0 == readernames.Length ) { //throw new InvalidOperationException( "There are currently no readers installed." ); Log.Warn( "There are currently no readers installed. Re-attempting in 10 seconds." ); if( null == RetryTimer ) { RetryTimer = new Timer( TimeSpan.FromSeconds( 10 ).TotalMilliseconds ); RetryTimer.Elapsed += ( e, args ) => { ExecuteContext(); }; RetryTimer.Start(); } } else { if( null != RetryTimer ) { RetryTimer.Stop(); RetryTimer.Dispose(); } // Create a monitor object with its own PC/SC context. monitor = new SCardMonitor( new SCardContext(), SCardScope.System ); // Point the callback function(s) to the static defined methods below. monitor.CardInserted += CardInserted; foreach( string reader in readernames ) { Log.InfoFormat( "Start monitoring for reader '{0}'.", reader ); } monitor.Start( readernames ); } // Wait for the parent application to signal us to exit. if( null == ExitApplication ) { ExitApplication = new ManualResetEvent( false ); } ExitApplication.WaitOne(); // Stop monitoring if( null != monitor ) { monitor.Cancel(); monitor.Dispose(); monitor = null; } } } catch( PCSCException pcscException ) { Log.Error( "Failed to run application", pcscException ); } }
private void startMonitor() { monitor = new SCardMonitor(new SCardContext(), SCardScope.System); monitor.Initialized += (sender, args) => ReaderInitializedEvent(args); monitor.CardInserted += (sender, args) => CardInsertedEvent(args); monitor.MonitorException += MonitorException; monitor.Start(readerNames); monitorRunning = true; }
public void Disconnect() { if (CardMonitor != null) { try { CardMonitor.Cancel(); CardMonitor.CardInserted -= CardInserted; CardMonitor.CardInserted -= CardRemoved; } catch (Exception) { } finally { CardMonitor = null; } } if (CardContext != null) { try { CardContext.Release(); } catch (Exception) { } finally { CardContext = null; } } }
public bool MonitorStart(string readerName) { try { _monitor = new SCardMonitor(new SCardContext(), SCardScope.System); _monitor.StatusChanged += (sender, args) => _StatusChanged(args); _monitor.CardRemoved += (sender, args) => _Removed("CardRemoved", args); _monitor.Start(readerName); return true; } catch (PCSCException ex) { _error_code = ECODE_SCardError; _error_message = "Err: " + ex.Message + " (" + ex.SCardError.ToString() + ")"; Console.WriteLine(_error_message); return false; } }