コード例 #1
0
        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);
            }
        }
コード例 #2
0
ファイル: CardReader.cs プロジェクト: edjo23/shop
        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");
            }
        }
コード例 #3
0
ファイル: CardReader.cs プロジェクト: darko0201/eVR
        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.");
        }
コード例 #4
0
 public void StopSmartCardMonitor()
 {
     if (_sCardMonitor != null)
     {
         _sCardMonitor.Cancel();
         _sCardMonitor = null;
     }
 }
コード例 #5
0
 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());
 }
コード例 #6
0
        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();
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: bestpetrovich/pcsc-sharp
        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();
        }
コード例 #8
0
ファイル: CardReader.cs プロジェクト: aboyce/RasPi-Control
        /// <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;
        }
コード例 #9
0
 // returns a new monitor each time.
 public SCardMonitor getMonitor()
 {
   SCardMonitor monitor = new SCardMonitor(ctx, scope);
   return monitor;
 }
コード例 #10
0
ファイル: DemoApplication.cs プロジェクト: hartwig-at/omniudp
        /// <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();
              }
        }
コード例 #11
0
ファイル: Application.cs プロジェクト: oliversalzburg/omniudp
        /// <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 );
            }
        }
コード例 #12
0
ファイル: PCSCReader.cs プロジェクト: pfirpfel/nfc-playground
 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;
 }
コード例 #13
0
ファイル: CardReader.cs プロジェクト: edjo23/shop
        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;
                }
            }
        }
コード例 #14
0
        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;
            }
        }