Beispiel #1
0
        /// <summary>Returns the current reader status of all requested readers.</summary>
        /// <param name="readerNames">Requested reader names.</param>
        /// <returns>An array of <see cref="T:PCSC.SCardReaderState" />s that contains the current reader status of each requested reader.</returns>
        /// <remarks>
        ///     <para>This method uses the <see cref="M:PCSC.SCardContext.GetStatusChange(System.IntPtr,PCSC.SCardReaderState[])" /> method with a timeout of zero.</para>
        ///     <example>
        ///         <code lang="C#">
        /// // Retrieve the names of all installed readers.
        /// using (var ctx = new SCardContext()) {
        ///     ctx.Establish(SCardScope.System);
        ///     var readerNames = ctx.GetReaders();
        ///
        ///     // Get the current status for all readers.
        ///     var states = ctx.GetReaderStatus(readerNames);
        ///
        ///     foreach (var state in states) {
        ///         Console.WriteLine("------------------------------");
        ///         Console.WriteLine("Reader: " + state.ReaderName);
        ///         Console.WriteLine("CurrentState: " + state.CurrentState
        ///             + " EventState: " + state.EventState + "\n"
        ///             + "CurrentStateValue: " + state.CurrentStateValue
        ///             + " EventStateValue: " + state.EventStateValue);
        ///         Console.WriteLine("UserData: " + state.UserData.ToString()
        ///             + " CardChangeEventCnt: " + state.CardChangeEventCnt);
        ///         Console.WriteLine("ATR: " + BitConverter.ToString(state.Atr));
        ///     }
        /// }
        ///   </code>
        ///     </example>
        /// </remarks>
        public SCardReaderState[] GetReaderStatus(string[] readerNames)
        {
            if (readerNames == null)
            {
                throw new ArgumentNullException("readerNames");
            }
            if (readerNames.Length == 0)
            {
                throw new ArgumentException("You must specify at least one reader.");
            }

            var states = new SCardReaderState[readerNames.Length];

            for (var i = 0; i < states.Length; i++)
            {
                states[i] = new SCardReaderState {
                    ReaderName   = readerNames[i],
                    CurrentState = SCRState.Unaware
                };
            }

            var rc = GetStatusChange(IntPtr.Zero, states);

            if (rc != SCardError.Success)
            {
                throw new PCSCException(rc, SCardHelper.StringifyError(rc));
            }

            return(states);
        }
Beispiel #2
0
        /// <summary>Returns a list of currently available reader groups on the system. </summary>
        /// <returns>An array of <see cref="T:System.String" />s containing all Smart Card reader groups found by the PC/SC Resource Manager.</returns>
        /// <remarks>
        ///     <para>This method calls the API function SCardListReaderGroups().</para>
        ///     <example>
        ///         <code lang="C#">
        /// using (var context = new SCardContext()) {
        ///     context.Establish(SCardScope.System);
        ///
        ///     // list all configured reader groups
        ///     Console.WriteLine("Currently configured readers groups: ");
        ///     var groups = context.GetReaderGroups();
        ///     foreach (string group in groups) {
        ///         Console.WriteLine("\t" + group);
        ///     }
        /// }
        ///   </code>
        ///     </example>
        /// </remarks>
        public string[] GetReaderGroups()
        {
            if (_contextPtr.Equals(IntPtr.Zero))
            {
                throw new InvalidContextException(SCardError.InvalidHandle);
            }

            string[] groups;

            var sc = Platform.Lib.ListReaderGroups(
                _contextPtr,
                out groups);

            switch (sc)
            {
            case SCardError.Success:
                return(groups);

            case SCardError.InvalidHandle:
                throw new InvalidContextException(sc, "Invalid Scope Handle");

            default:
                throw new PCSCException(sc, SCardHelper.StringifyError(sc));
            }
        }
Beispiel #3
0
        /// <summary>Creates an Application Context to the PC/SC Resource Manager.</summary>
        /// <param name="scope">Scope of the establishment. This can either be a local or remote connection.</param>
        /// <remarks>
        ///     <para>Creates an Application Context for a client. This must be the first WinSCard function called in a PC/SC application. Each thread of an application shall use its own <see cref="T:PCSC.SCardContext" />.</para>
        ///     <para>This method calls the API function SCardEstablishContext().</para>
        ///     <example>
        ///         <code lang="C#">
        /// var context = new SCardContext();
        /// context.Establish(SCardScope.System);
        ///   </code>
        ///     </example>
        /// </remarks>
        public void Establish(SCardScope scope)
        {
            if (_hasContext && IsValid())
            {
                Release();
            }

            IntPtr hContext;

            var rc = Platform.Lib.EstablishContext(
                scope,
                IntPtr.Zero,
                IntPtr.Zero,
                out hContext);

            switch (rc)
            {
            case SCardError.Success:
                _contextPtr = hContext;
                _lastScope  = scope;
                _hasContext = true;
                break;

            case SCardError.InvalidValue:
                throw new InvalidScopeTypeException(rc, "Invalid scope type passed");

            default:
                throw new PCSCException(rc, SCardHelper.StringifyError(rc));
            }
        }
Beispiel #4
0
        public string[] GetReaderGroups()
        {
            if (contextPtr.Equals(IntPtr.Zero))
            {
                throw new InvalidContextException(SCardError.InvalidHandle);
            }

            string[] groups;

            SCardError sc = SCardAPI.Lib.ListReaderGroups(
                contextPtr,
                out groups);

            if (sc == SCardError.Success)
            {
                return(groups);
            }
            else if (sc == SCardError.InvalidHandle)
            {
                throw new InvalidContextException(sc, "Invalid Scope Handle");
            }
            else
            {
                throw new PCSCException(sc, SCardHelper.StringifyError(sc));
            }
        }
Beispiel #5
0
        public string[] GetReaders(string[] Groups)
        {
            if (contextPtr.Equals(IntPtr.Zero))
            {
                throw new InvalidContextException(SCardError.InvalidHandle);
            }

            SCardError rc;

            string[] readers;

            rc = SCardAPI.Lib.ListReaders(
                contextPtr,
                Groups,
                out readers);

            if (rc == SCardError.Success)
            {
                return(readers);
            }
            if (rc == SCardError.InvalidHandle)
            {
                throw new InvalidContextException(rc, "Invalid Scope Handle");
            }
            else
            {
                throw new PCSCException(rc, SCardHelper.StringifyError(rc));
            }
        }
Beispiel #6
0
        public void Release()
        {
            if (!hasContext)
            {
                throw new InvalidContextException(SCardError.UnknownError, "Context was not established");
            }

            SCardError rc;

            rc = SCardAPI.Lib.ReleaseContext(contextPtr);

            if (rc == SCardError.Success)
            {
                contextPtr = IntPtr.Zero;
                hasContext = false;
            }
            else
            {
                if (rc == SCardError.InvalidHandle)
                {
                    throw new InvalidContextException(rc, "Invalid Context handle");
                }
                else
                {
                    throw new PCSCException(rc, SCardHelper.StringifyError(rc));
                }
            }
        }
Beispiel #7
0
        public void Establish(SCardScope scope)
        {
            if (hasContext)
                if (IsValid())
                    Release();

            SCardError rc;
            IntPtr hContext = IntPtr.Zero;

            rc = SCardAPI.Lib.EstablishContext(scope,
                IntPtr.Zero,
                IntPtr.Zero,
                ref hContext);

            if (rc == SCardError.Success)
            {
                contextPtr = hContext;
                lastScope = scope;
                hasContext = true;
            }
            else
            {
                if (rc == SCardError.InvalidValue)
                    throw new InvalidScopeTypeException(rc, "Invalid scope type passed");
                else
                    throw new PCSCException(rc, SCardHelper.StringifyError(rc));
            }

        }
Beispiel #8
0
        public SCardError GetAttrib(
            IntPtr AttrId,
            out byte[] pbAttr)
        {
            int attrlen;

            // receive needed size for pbAttr
            SCardError rc = GetAttrib(AttrId, null, out attrlen);

            if (rc != SCardError.Success)
            {
                throw new PCSCException(rc, SCardHelper.StringifyError(rc));
            }

            pbAttr = new byte[attrlen];

            return(GetAttrib(AttrId, pbAttr, out attrlen));
        }
Beispiel #9
0
        /// <summary>Destroys a communication context to the PC/SC Resource Manager and frees unmanaged resources.</summary>
        /// <remarks>
        ///     <para>This must be the last method called in a PC/SC application. <see cref="Dispose()"/> calls this method silently.</para>
        ///     <para>This method calls the API function SCardReleaseContext().</para>
        ///     <example>
        ///         <code lang="C#">
        /// var context = new SCardContext();
        ///
        /// // establish context
        /// context.Establish(SCardScope.System);
        ///
        /// // release context
        /// context.Release();
        ///   </code>
        ///     </example>
        /// </remarks>
        public void Release()
        {
            if (!_hasContext)
            {
                throw new InvalidContextException(SCardError.UnknownError, "Context was not established");
            }

            var rc = Platform.Lib.ReleaseContext(_contextPtr);

            switch (rc)
            {
            case SCardError.Success:
                _contextPtr = IntPtr.Zero;
                _hasContext = false;
                break;

            case SCardError.InvalidHandle:
                throw new InvalidContextException(rc, "Invalid Context handle");

            default:
                throw new PCSCException(rc, SCardHelper.StringifyError(rc));
            }
        }
Beispiel #10
0
        public SCardReaderState[] GetReaderStatus(string[] readernames)
        {
            SCardError rc;

            if (readernames == null)
                throw new ArgumentNullException("readernames");
            if (readernames.Length == 0)
                throw new ArgumentException("You must specify at least one reader.");

            SCardReaderState[] states = new SCardReaderState[readernames.Length];
            for (int i = 0; i < states.Length; i++)
            {
                states[i] = new SCardReaderState();
                states[i].ReaderName = readernames[i];
                states[i].CurrentState = SCRState.Unaware;
            }

            rc = GetStatusChange(IntPtr.Zero, states);
            if (rc != SCardError.Success)
                throw new PCSCException(rc, SCardHelper.StringifyError(rc));

            return states;
        }
 public InvalidContextException(SCardError serr)
     : base(serr, SCardHelper.StringifyError(serr))
 {
 }
Beispiel #12
0
 /// <summary>
 /// Creates a new instance.
 /// </summary>
 /// <param name="serr">System's error code</param>
 public PCSCException(SCardError serr)
     : base(SCardHelper.StringifyError(serr))
 {
     SCardError = serr;
 }