Beispiel #1
0
        public SCardError Reconnect(SCardShareMode mode,
                                    SCardProtocol prefProto,
                                    SCardReaderDisposition initExec)
        {
            if (cardHandle.Equals(IntPtr.Zero))
            {
                throw new InvalidOperationException(
                          "Reader is currently not connected or no card handle has been returned.");
            }

            SCardProtocol dwActiveProtocol = activeprot;

            SCardError rc = SCardAPI.Lib.Reconnect(cardHandle,
                                                   mode,
                                                   prefProto,
                                                   initExec,
                                                   out dwActiveProtocol);


            if (rc == SCardError.Success)
            {
                activeprot = dwActiveProtocol;
                sharemode  = mode;
            }

            return(rc);
        }
Beispiel #2
0
        public SCardError Connect(
            IntPtr hContext,
            string szReader,
            SCardShareMode dwShareMode,
            SCardProtocol dwPreferredProtocols,
            out IntPtr phCard,
            out SCardProtocol pdwActiveProtocol)
        {
            byte[] readername = SCardHelper._ConvertToByteArray(szReader, textEncoding, SCardAPI.Lib.CharSize);
            Int32  sharemode  = (Int32)dwShareMode;
            Int32  prefproto  = (Int32)dwPreferredProtocols;
            IntPtr ctx        = (IntPtr)hContext;
            IntPtr card;
            Int32  activeproto;

            Int32 result = SCardConnect(ctx,
                                        readername,
                                        sharemode,
                                        prefproto,
                                        out card,
                                        out activeproto);

            phCard            = card;
            pdwActiveProtocol = (SCardProtocol)activeproto;

            return(SCardHelper.ToSCardError(result));
        }
Beispiel #3
0
        /// <inheritdoc />
        public void Connect(string readerName, SCardShareMode mode, SCardProtocol preferredProtocol)
        {
            ThrowIfDisposed();
            ThrowIfAlreadyConnected();

            if (readerName == null)
            {
                throw new ArgumentNullException(nameof(readerName));
            }

            if (string.IsNullOrWhiteSpace(readerName))
            {
                throw new UnknownReaderException(SCardError.InvalidValue, "Invalid card reader name.");
            }

            if (_context.Handle.Equals(IntPtr.Zero))
            {
                throw new InvalidContextException(SCardError.InvalidHandle, "Invalid connection context.");
            }

            _api.Connect(
                hContext: _context.Handle,
                szReader: readerName,
                dwShareMode: mode,
                dwPreferredProtocols: preferredProtocol,
                phCard: out var cardHandle,
                pdwActiveProtocol: out var activeProtocol)
            .ThrowIfNotSuccess();

            Handle     = cardHandle;
            Protocol   = activeProtocol;
            Mode       = mode;
            ReaderName = readerName;
        }
Beispiel #4
0
        public IsoCard(ISCardReader reader, string readerName, SCardShareMode mode, SCardProtocol proto)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            this.reader = reader;
            Connect(readerName, mode, proto);
        }
Beispiel #5
0
        /// <inheritdoc />
        public ICardHandle Connect(string readerName, SCardShareMode mode, SCardProtocol preferredProtocol)
        {
            ThrowOnInvalidContext();

            var handle = new CardHandle(_api, this);

            handle.Connect(readerName, mode, preferredProtocol);
            return(handle);
        }
Beispiel #6
0
        public IsoCard(ISCardReader reader, string readerName, SCardShareMode mode, SCardProtocol proto)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            this.reader = reader;
            Connect(readerName, mode, proto);
        }
Beispiel #7
0
        public SCardError Reconnect(IntPtr hCard, SCardShareMode dwShareMode, SCardProtocol dwPreferredProtocols, SCardReaderDisposition dwInitialization, out SCardProtocol pdwActiveProtocol)
        {
            var result = SCardReconnect(
                hCard,
                (int)dwShareMode,
                (int)dwPreferredProtocols,
                (int)dwInitialization,
                out var activeproto);

            pdwActiveProtocol = (SCardProtocol)activeproto;
            return(SCardHelper.ToSCardError(result));
        }
Beispiel #8
0
        /// <summary>
        /// Creates a new <see cref="CCIDDriver"/> by connecting to the device specified by <paramref name="name"/>.
        /// </summary>
        /// <param name="name">Device name</param>
        /// <param name="mode">Connection mode</param>
        /// <param name="protocol">Connection protocol</param>
        /// <returns>Returns a new <see cref="CCIDDriver"/> if the device was found.</returns>
        /// <exception cref="ConnectionException">Thrown if the device is not found or may not be connected to at this time.</exception>
        public static CCIDDriver OpenDevice(string name, SCardShareMode mode = SCardShareMode.Exclusive, SCardProtocol protocol = SCardProtocol.Any)
        {
            ISCardContext ctx    = ContextFactory.Instance.Establish(SCardScope.System);
            ISCardReader  reader = new SCardReader(ctx);

            if (reader.Connect(name, mode, protocol) != SCardError.Success)
            {
                throw new ConnectionException("Failed to connect to device.");
            }

            return(new CCIDDriver(ctx, reader));
        }
Beispiel #9
0
        public IntPtr Connect(string readerName, SCardShareMode shareMode, SCardProtocol protocol)
        {
            IntPtr        CardHandle;
            SCardProtocol ActiveProtocol;
            SCardError    Error = SCardApi.SCardConnect(this.Context, readerName, shareMode, protocol, out CardHandle, out ActiveProtocol);

            if (SCardApi.IsError(Error))
            {
                throw new SCardException(Error);
            }

            SCardApi.sessionProtocols.Add(CardHandle, protocol);
            return(CardHandle);
        }
Beispiel #10
0
        public virtual void Connect(string readerName, SCardShareMode mode, SCardProtocol proto)
        {
            if (readerName == null)             // Invalid reader name?
                throw new ArgumentNullException("readerName");  
            if (proto == SCardProtocol.Unset)   // Invalid protocol?
                throw new InvalidProtocolException(SCardError.InvalidValue);
            if (((long)mode) == 0)              // Invalid sharing mode?
                throw new InvalidShareModeException(SCardError.InvalidValue);
            
            SCardError sc = reader.Connect(readerName, mode, proto);

            // Throws an exception if sc != SCardError.Success
            ThrowExceptionOnSCardError(sc);
        }
Beispiel #11
0
        public SCardError Connect(IntPtr hContext, string szReader, SCardShareMode dwShareMode, SCardProtocol dwPreferredProtocols, out IntPtr phCard, out SCardProtocol pdwActiveProtocol)
        {
            var readername = SCardHelper.ConvertToByteArray(szReader, TextEncoding, Platform.Lib.CharSize);

            var result = SCardConnect(hContext,
                                      readername,
                                      (int)dwShareMode,
                                      (int)dwPreferredProtocols,
                                      out phCard,
                                      out var activeproto);

            pdwActiveProtocol = (SCardProtocol)activeproto;

            return(SCardHelper.ToSCardError(result));
        }
Beispiel #12
0
        public void Reconnect(IntPtr cardHandle, SCardDisposition disposition, SCardShareMode shareMode, SCardProtocol protocol)
        {
            if (disposition == SCardDisposition.Eject)
            {
                throw new ArgumentException("Disposition.Eject not possible in reconnect", nameof(disposition));
            }
            SCardProtocol ActiveProtocol;
            SCardError    Error = SCardApi.SCardReconnect(cardHandle, shareMode, protocol, disposition, out ActiveProtocol);

            if (SCardApi.IsError(Error))
            {
                throw new SCardException(Error);
            }

            SCardApi.sessionProtocols.Remove(cardHandle);
            SCardApi.sessionProtocols.Add(cardHandle, protocol);
        }
Beispiel #13
0
        /// <inheritdoc />
        public void Reconnect(SCardShareMode mode, SCardProtocol preferredProtocol,
                              SCardReaderDisposition initialExecution)
        {
            ThrowIfDisposed();
            ThrowOnInvalidCardHandle();

            _api.Reconnect(
                hCard: Handle,
                dwShareMode: mode,
                dwPreferredProtocols: preferredProtocol,
                dwInitialization: initialExecution,
                pdwActiveProtocol: out var dwActiveProtocol)
            .ThrowIfNotSuccess();

            Protocol = dwActiveProtocol;
            Mode     = mode;
        }
Beispiel #14
0
        public virtual void Connect(string readerName, SCardShareMode mode, SCardProtocol proto)
        {
            if (readerName == null)             // Invalid reader name?
            {
                throw new ArgumentNullException("readerName");
            }
            if (proto == SCardProtocol.Unset)   // Invalid protocol?
            {
                throw new InvalidProtocolException(SCardError.InvalidValue);
            }
            if (((long)mode) == 0)              // Invalid sharing mode?
            {
                throw new InvalidShareModeException(SCardError.InvalidValue);
            }

            SCardError sc = reader.Connect(readerName, mode, proto);

            // Throws an exception if sc != SCardError.Success
            ThrowExceptionOnSCardError(sc);
        }
        /// <inheritdoc />
        public SCardError Reconnect(SCardShareMode mode, SCardProtocol preferredProtocol,
                                    SCardReaderDisposition initialExecution)
        {
            ThrowOnInvalidCardHandle();

            var rc = Platform.Lib.Reconnect(_cardHandle,
                                            mode,
                                            preferredProtocol,
                                            initialExecution,
                                            out var dwActiveProtocol);

            if (rc != SCardError.Success)
            {
                return(rc);
            }

            ActiveProtocol   = dwActiveProtocol;
            CurrentShareMode = mode;

            return(rc);
        }
Beispiel #16
0
        /// <summary>Connects the specified reader.</summary>
        /// <param name="readerName">Name of the reader.</param>
        /// <param name="mode">The share mode.</param>
        /// <param name="protocol">The communication protocol. <seealso cref="ISCardReader.Connect(string,SCardShareMode,SCardProtocol)" /></param>
        public virtual void Connect(string readerName, SCardShareMode mode, SCardProtocol protocol)
        {
            if (readerName == null)
            {
                throw new ArgumentNullException(nameof(readerName));
            }

            if (protocol == SCardProtocol.Unset)
            {
                throw new InvalidProtocolException(SCardError.InvalidValue);
            }

            if (((long)mode) == 0)
            {
                throw new InvalidShareModeException(SCardError.InvalidValue);
            }

            var sc = Reader.Connect(readerName, mode, protocol);

            // Throws an exception if sc != SCardError.Success
            sc.ThrowIfNotSuccess();
        }
        /// <inheritdoc />
        public SCardError Connect(string readerName, SCardShareMode mode, SCardProtocol preferredProtocol)
        {
            if (readerName == null)
            {
                throw new ArgumentNullException(nameof(readerName));
            }

            if (string.IsNullOrWhiteSpace(readerName))
            {
                throw new UnknownReaderException(SCardError.InvalidValue, "Invalid card reader name.");
            }

            if (CurrentContext == null || CurrentContext.Handle.Equals(IntPtr.Zero))
            {
                throw new InvalidContextException(SCardError.InvalidHandle, "Invalid connection context.");
            }

            var rc = Platform.Lib.Connect(CurrentContext.Handle,
                                          readerName,
                                          mode,
                                          preferredProtocol,
                                          out var hCard,
                                          out var dwActiveProtocol);

            if (rc != SCardError.Success)
            {
                return(rc);
            }

            _cardHandle      = hCard;
            ActiveProtocol   = dwActiveProtocol;
            ReaderName       = readerName;
            CurrentShareMode = mode;

            return(rc);
        }
Beispiel #18
0
        public SCardError Connect(string name,
                                  SCardShareMode mode,
                                  SCardProtocol prefProto)
        {
            if (name == null ||
                name.Equals(""))
            {
                throw new UnknownReaderException(SCardError.InvalidValue, "Invalid card reader name.");
            }

            if (context == null || context.contextPtr.Equals(IntPtr.Zero))
            {
                throw new InvalidContextException(SCardError.InvalidHandle, "Invalid connection context.");
            }

            SCardError    rc;
            IntPtr        hCard = IntPtr.Zero;
            SCardProtocol dwActiveProtocol;

            rc = SCardAPI.Lib.Connect(context.contextPtr,
                                      name,
                                      mode,
                                      prefProto,
                                      out hCard,
                                      out dwActiveProtocol);

            if (rc == SCardError.Success)
            {
                cardHandle = hCard;
                activeprot = dwActiveProtocol;
                readername = name;
                sharemode  = mode;
            }

            return(rc);
        }
Beispiel #19
0
 public static extern PCSCResult SCardReconnect(IntPtr hCard, SCardShareMode dwShareMode,
                                                SCardProtocol dwPrefProtocol, SCardDisposition dwInitialization, ref SCardProtocol ActiveProtocol);
Beispiel #20
0
        public void Connect(SCardShareMode shareMode = SCardShareMode.Shared)
        {
            PCSCResult result;

            // Are we already connected?
            if (myHandle != IntPtr.Zero)
            {
                return;
            }

            // Establish a new session context for this card
            result = NativeMethods.SCardEstablishContext(SCardScope.User, IntPtr.Zero, IntPtr.Zero, out myContext);
            if (PCSCResult.None != result)
            {
                throw PCSCException.ThrowByResult(result);
            }

            // Connect to the card
            result = NativeMethods.SCardConnect(myContext, myReaderName, shareMode, DefaultRequestedProtocols, ref myHandle, ref myCommsProtocol);

            if (PCSCResult.None != result)
            {
                // Disconnect (this will gracefully handle the context release)
                Disconnect();
                throw PCSCException.ThrowByResult(result);
            }

            // Retrieve our ATR
            byte[]        readerName   = new byte[255];
            uint          readerLen    = 255;
            SCardState    state        = 0;
            SCardProtocol cardProtocol = 0;
            uint          atrLen       = NativeMethods.SCARD_ATR_LENGTH;

            byte[] atr = new byte[atrLen];

            result = NativeMethods.SCardStatus(myHandle, readerName, ref readerLen, out state, out cardProtocol, atr, ref atrLen);
            if (result != PCSCResult.None)
            {
                // Disconnect from the card
                Disconnect();
                throw PCSCException.ThrowByResult(result);
            }


            //
            // Protocol Enumeration
            //
            try
            {
                // Add the default 7816 protocol
                Protocols.Add(new PCSCIso7816Protocol(atr, this));

                // Determine whether this card is contactless or not
                if (SupportsIso14443)
                {
                    // Replace the 7816 protocol with 14443
                    Protocols.Clear();

                    var protocol = new PCSCIso14443AProtocol(atr, this);
                    Protocols.Add(protocol);

                    // Create our OptionalCommands object
                    OptionalCommands = new PCSCOptionalCommands(protocol);

                    //
                    // HACK: Adding a PCSCMifareProtocol to all ISO14443 cards, what
                    //       we should be doing is somehow determining if this card
                    //       supports Mifare or Mifare emulation
                    //
                    Protocols.Add(new PCSCMifareProtocol(this));
                }
            }
            catch (Exception)
            {
                // Disconnect from the card
                Disconnect();
                throw PCSCException.ThrowUnsupportedProtocol();
            }
        }
Beispiel #21
0
        public SCardError Connect(IntPtr hContext, string szReader, SCardShareMode dwShareMode, SCardProtocol dwPreferredProtocols, out IntPtr phCard, out SCardProtocol pdwActiveProtocol) {
            var readername = SCardHelper.ConvertToByteArray(szReader, _textEncoding, Platform.Lib.CharSize);
            IntPtr activeproto;

            var result = SCardConnect(hContext,
                readername,
                (IntPtr) dwShareMode,
                (IntPtr) dwPreferredProtocols,
                out phCard,
                out activeproto);

            pdwActiveProtocol = (SCardProtocol) activeproto;

            return SCardHelper.ToSCardError(result);
        }
Beispiel #22
0
 public SCardError Connect(string readerName, SCardShareMode mode, SCardProtocol preferredProtocol)
 {
     throw new NotImplementedException();
 }
Beispiel #23
0
        /// <inheritdoc />
        public ICardReader ConnectReader(string readerName, SCardShareMode mode, SCardProtocol preferredProtocol)
        {
            var handle = Connect(readerName, mode, preferredProtocol);

            return(new CardReader(_api, handle, true));
        }
Beispiel #24
0
 public SCardError Reconnect(SCardShareMode mode, SCardProtocol preferredProtocol, SCardReaderDisposition initialExecution)
 {
     throw new NotImplementedException();
 }
Beispiel #25
0
        /// <summary>
        /// Connects to a card on the specified reader using the specified parameters.
        /// </summary>
        /// <param name="context">An IntPtr that is the context to use.</param>
        /// <param name="readerName">A string containing the name of the reader to use.</param>
        /// <param name="cardHandle">An IntPtr that will be set to the handle of the card.</param>
        /// <param name="actualProtocol">A SCardProtocol that will be set to the established protocol.</param>
        /// <param name="result">A SCardResponse that will be set to the result of the operation.</param>
        /// <param name="shareMode">A SCardShareMode that indicates other applications can connect to the card.</param>
        /// <param name="protocol">A SCardProtocol indicating the acceptable protocols to use for the connection.</param>
        /// <returns>A bool that indicates whether the operation was a success.</returns>
        internal static bool ConnectCard(IntPtr context, string readerName, out IntPtr cardHandle, out SCardProtocol actualProtocol, out SCardResponse result, SCardShareMode shareMode = SCardShareMode.Shared, SCardProtocol protocol = SCardProtocol.T0)
        {
            uint resultCode;
            bool returnValue = false;

            resultCode = SCardConnect(context, readerName, shareMode, protocol, out cardHandle, out actualProtocol);
            result     = (SCardResponse)resultCode;
            if (result == SCardResponse.SCARD_S_SUCCESS)
            {
                returnValue = true;
            }

            return(returnValue);
        }
Beispiel #26
0
        /// <summary>Terminates a connection made through <see cref="M:PCSC.ISCardReader.Connect(System.String,PCSC.SCardShareMode,PCSC.SCardProtocol)" />.</summary>
        /// <param name="disconnectExecution">Reader function to execute.</param>
        /// <returns>
        ///     <list type="table">
        ///         <listheader>
        ///             <term>Return value</term>
        ///             <description>Description</description>
        ///         </listheader>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.Success" />
        ///             </term>
        ///             <description>Successful (SCARD_S_SUCCESS)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.InvalidHandle" />
        ///             </term>
        ///             <description>Invalid card handle (SCARD_E_INVALID_HANDLE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.InvalidValue" />
        ///             </term>
        ///             <description>Invalid <paramref name="disconnectExecution" /> (SCARD_E_INVALID_VALUE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.NoService" />
        ///             </term>
        ///             <description>The server is not runing (SCARD_E_NO_SERVICE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.NoSmartcard" />
        ///             </term>
        ///             <description>No smart card present (SCARD_E_NO_SMARTCARD)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.CommunicationError" />
        ///             </term>
        ///             <description>An internal communications error has been detected (SCARD_F_COMM_ERROR)</description>
        ///         </item>
        ///     </list>
        /// </returns>
        /// <remarks>
        ///     <para>This method calls the API function SCardDisconnect().</para>
        ///     <example>
        ///         <code lang="C#">
        /// // Establish PC/SC context.
        /// SCardContext ctx = new SCardContext();
        /// ctx.Establish(SCardScope.System);
        /// 
        /// // Create a Smart Card reader object and connect to it.
        /// ISCardReader reader = new SCardReader(ctx);
        /// SCardError serr = reader.Connect("OMNIKEY", SCardShareMode.Shared, SCardProtocol.Any);
        /// 
        /// // Disconnect the reader and reset the SmartCard.
        /// reader.Disconnect(SCardReaderDisposition.Reset);
        ///   </code>
        ///     </example>
        /// </remarks>
        public SCardError Disconnect(SCardReaderDisposition disconnectExecution)
        {
            ThrowOnInvalidCardHandle();

            var rc = Platform.Lib.Disconnect(_cardHandle, disconnectExecution);

            if (rc != SCardError.Success) {
                return rc;
            }

            // reset local variables
            _readername = null;
            _cardHandle = IntPtr.Zero;
            _activeprot = SCardProtocol.Unset;
            _sharemode = SCardShareMode.Shared;

            return rc;
        }
Beispiel #27
0
        public SCardError Disconnect(SCardReaderDisposition discntExec)
        {
            if (cardHandle.Equals(IntPtr.Zero))
                throw new InvalidOperationException(
                    "Reader is currently not connected or no card handle has been returned.");

            SCardError rc = SCardAPI.Lib.Disconnect(cardHandle,
                discntExec);

            if (rc == SCardError.Success)
            {
                // reset local variables
                readername = null;
                cardHandle = IntPtr.Zero;
                activeprot = SCardProtocol.Unset;
                sharemode = SCardShareMode.Shared;
            }

            return rc;
        }
Beispiel #28
0
        public SCardError Connect(
            IntPtr hContext,
            string szReader,
            SCardShareMode dwShareMode,
            SCardProtocol dwPreferredProtocols,
            out IntPtr phCard,
            out SCardProtocol pdwActiveProtocol)
        {
            byte[] readername = SCardHelper._ConvertToByteArray(szReader, textEncoding, SCardAPI.Lib.CharSize);
            IntPtr ctx = (IntPtr)hContext;
            IntPtr sharemode = (IntPtr)dwShareMode;
            IntPtr prefproto = (IntPtr)dwPreferredProtocols;
            IntPtr card;
            IntPtr activeproto;

            IntPtr result = SCardConnect(ctx,
                readername,
                sharemode,
                prefproto,
                out card,
                out activeproto);

            phCard = card;
            pdwActiveProtocol = (SCardProtocol)activeproto;

            return SCardHelper.ToSCardError(result);
        }
Beispiel #29
0
        /// <summary>Reestablishes a connection to a reader that was previously connected to using
        ///     <see
        ///         cref="M:PCSC.ISCardReader.Connect(System.String,PCSC.SCardShareMode,PCSC.SCardProtocol)" />
        ///     .</summary>
        /// <param name="mode">Mode of connection type: exclusive/shared.
        ///     <list type="table">
        ///         <listheader>
        ///             <term>Value</term><description>Description</description>
        ///         </listheader>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardShareMode.Shared" />
        ///             </term>
        ///             <description>This application will allow others to share the reader. (SCARD_SHARE_SHARED)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardShareMode.Exclusive" />
        ///             </term>
        ///             <description>This application will NOT allow others to share the reader. (SCARD_SHARE_EXCLUSIVE)</description>
        ///         </item>
        ///     </list>
        /// </param>
        /// <param name="preferredProtocol">Desired protocol use.</param>
        /// <param name="initialExecution">Desired action taken on the card/reader before reconnect.</param>
        /// <returns>An error code / return value:
        ///     <list type="table">
        ///         <listheader>
        ///             <term>Error code</term><description>Description</description>
        ///         </listheader>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.Success" />
        ///             </term>
        ///             <description>Successful (SCARD_S_SUCCESS)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.InvalidHandle" />
        ///             </term>
        ///             <description>Invalid context handle (SCARD_E_INVALID_HANDLE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.InvalidParameter" />
        ///             </term>
        ///             <description>
        ///                 <paramref name="preferredProtocol" /> is invalid or <see langword="null" />  (SCARD_E_INVALID_PARAMETER)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.InvalidValue" />
        ///             </term>
        ///             <description>Invalid sharing mode, requested protocol, or reader name (SCARD_E_INVALID_VALUE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.NoService" />
        ///             </term>
        ///             <description>The server is not runing (SCARD_E_NO_SERVICE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.NoSmartcard" />
        ///             </term>
        ///             <description>No smart card present (SCARD_E_NO_SMARTCARD)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.NotReady" />
        ///             </term>
        ///             <description>Could not allocate the desired port (SCARD_E_NOT_READY)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.ProtocolMismatch" />
        ///             </term>
        ///             <description>Requested protocol is unknown (SCARD_E_PROTO_MISMATCH)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.ReaderUnavailable" />
        ///             </term>
        ///             <description>Could not power up the reader or card (SCARD_E_READER_UNAVAILABLE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.SharingViolation" />
        ///             </term>
        ///             <description>Someone else has exclusive rights (SCARD_E_SHARING_VIOLATION)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.UnsupportedFeature" />
        ///             </term>
        ///             <description>Protocol not supported (SCARD_E_UNSUPPORTED_FEATURE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.CommunicationError" />
        ///             </term>
        ///             <description>An internal communications error has been detected (SCARD_F_COMM_ERROR)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.InternalError" />
        ///             </term>
        ///             <description>An internal consistency check failed (SCARD_F_INTERNAL_ERROR)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.RemovedCard" />
        ///             </term>
        ///             <description>The smart card has been removed (SCARD_W_REMOVED_CARD)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.UnresponsiveCard" />
        ///             </term>
        ///             <description>Card is mute (SCARD_W_UNRESPONSIVE_CARD)</description>
        ///         </item>
        ///     </list>
        /// </returns>
        /// <remarks>
        ///     <para>
        ///         <paramref name="preferredProtocol" />  is a bit mask of acceptable protocols for the connection. You can use (<see cref="F:PCSC.SCardProtocol.T0" /> | <see cref="F:PCSC.SCardProtocol.T1" />) if you do not have a preferred protocol. The protocol used with this connection will be stored in <see cref="P:PCSC.ISCardReader.ActiveProtocol" />.</para>
        ///     <para>This method calls the API function SCardReconnect().</para>
        /// </remarks>
        public SCardError Reconnect(SCardShareMode mode, SCardProtocol preferredProtocol,
            SCardReaderDisposition initialExecution)
        {
            ThrowOnInvalidCardHandle();

            SCardProtocol dwActiveProtocol;
            var rc = Platform.Lib.Reconnect(_cardHandle,
                mode,
                preferredProtocol,
                initialExecution,
                out dwActiveProtocol);

            if (rc != SCardError.Success) {
                return rc;
            }

            _activeprot = dwActiveProtocol;
            _sharemode = mode;

            return rc;
        }
Beispiel #30
0
 public static extern PCSCResult SCardConnect(IntPtr hContext, string cReaderName,
                                              SCardShareMode dwShareMode, SCardProtocol dwPrefProtocol, ref IntPtr phCard, ref SCardProtocol ActiveProtocol);
 /// <inheritdoc />
 public void Reconnect(SCardShareMode mode, SCardProtocol preferredProtocol,
                       SCardReaderDisposition initialExecution)
 {
     CardHandle.Reconnect(mode, preferredProtocol, initialExecution);
 }
Beispiel #32
0
 private static extern SCardError SCardConnect(IntPtr hContext, string szReader, SCardShareMode dwShareMode, SCardProtocol dwPreferredProtocols, [Out] out IntPtr phCard,
                                               [Out] out SCardProtocol pdwActiveProtocol);
Beispiel #33
0
 /// <summary>Initializes a new instance of the <see cref="IsoReader" /> class that will create its own instance of a <see cref="SCardReader" /> and immediately connect.</summary>
 /// <param name="context">A context to the PC/SC Resource Manager.</param>
 /// <param name="readerName">Name of the reader to connect with.</param>
 /// <param name="mode">The share mode.</param>
 /// <param name="protocol">The communication protocol. <seealso cref="ISCardReader.Connect(string,SCardShareMode,SCardProtocol)" /></param>
 /// <param name="releaseContextOnDispose">if set to <c>true</c> the <paramref name="context" /> will be released on <see cref="Dispose()" />.</param>
 public IsoReader(ISCardContext context, string readerName, SCardShareMode mode, SCardProtocol protocol,
                  bool releaseContextOnDispose = true)
     : this(context, releaseContextOnDispose)
 {
     Connect(readerName, mode, protocol);
 }
Beispiel #34
0
 /// <summary>Initializes a new instance of the <see cref="IsoReader" /> class and immediately connects to the reader.</summary>
 /// <param name="reader">The supplied reader will be used for communication with the smart card.</param>
 /// <param name="readerName">Name of the reader to connect with.</param>
 /// <param name="mode">The share mode.</param>
 /// <param name="protocol">The communication protocol. <seealso cref="ISCardReader.Connect(string,SCardShareMode,SCardProtocol)" /></param>
 /// <param name="disconnectReaderOnDispose">if set to <c>true</c> the supplied <paramref name="reader" /> will be disconnected on <see cref="Dispose()" />.</param>
 public IsoReader(ISCardReader reader, string readerName, SCardShareMode mode, SCardProtocol protocol,
                  bool disconnectReaderOnDispose = true)
     : this(reader, disconnectReaderOnDispose)
 {
     Connect(readerName, mode, protocol);
 }
Beispiel #35
0
        public SCardError Reconnect(
            IntPtr hCard,
            SCardShareMode dwShareMode,
            SCardProtocol dwPreferredProtocols,
            SCardReaderDisposition dwInitialization,
            out SCardProtocol pdwActiveProtocol)
        {
            IntPtr activeproto;
            IntPtr result = SCardReconnect(
                (IntPtr)hCard,
                (IntPtr)dwShareMode,
                (IntPtr)dwPreferredProtocols,
                (IntPtr)dwInitialization,
                out activeproto);

            pdwActiveProtocol = (SCardProtocol)activeproto;
            return SCardHelper.ToSCardError(result);
        }
Beispiel #36
0
 /// <summary>Initializes a new instance of the <see cref="IsoReader" /> class that will create its own instance of a <see cref="SCardReader" /> and immediately connect.</summary>
 /// <param name="context">A context to the PC/SC Resource Manager.</param>
 /// <param name="readerName">Name of the reader to connect with.</param>
 /// <param name="mode">The share mode.</param>
 /// <param name="protocol">The communication protocol. <seealso cref="ISCardReader.Connect(string,SCardShareMode,SCardProtocol)" /></param>
 /// <param name="releaseContextOnDispose">if set to <c>true</c> the <paramref name="context" /> will be released on <see cref="Dispose()" />.</param>
 public IsoReader(ISCardContext context, string readerName, SCardShareMode mode, SCardProtocol protocol,
     bool releaseContextOnDispose = true)
     : this(context, releaseContextOnDispose) {
     Connect(readerName, mode, protocol);
 }
Beispiel #37
0
        /// <summary>Establishes a connection to the Smart Card reader.</summary>
        /// <param name="readerName">Reader name to connect to.</param>
        /// <param name="mode">Mode of connection type: exclusive or shared.
        ///     <list type="table">
        ///         <listheader>
        ///             <term>Value</term><description>Description</description>
        ///         </listheader>
        ///         <item>
        ///             <term><see cref="F:PCSC.SCardShareMode.Shared" /></term>
        ///             <description>This application will allow others to share the reader. (SCARD_SHARE_SHARED)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardShareMode.Exclusive" />
        ///             </term>
        ///             <description>This application will NOT allow others to share the reader. (SCARD_SHARE_EXCLUSIVE)</description>
        ///         </item>
        ///     </list>
        /// </param>
        /// <param name="preferredProtocol">Desired protocol use.</param>
        /// <returns>An error code / return value:
        ///     <para>
        ///         <list type="table">
        ///             <listheader>
        ///                 <term>Error code</term><description>Description</description>
        ///             </listheader>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.Success" />
        ///                 </term>
        ///                 <description>Successful (SCARD_S_SUCCESS)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.InvalidHandle" />
        ///                 </term>
        ///                 <description>Invalid context handle (SCARD_E_INVALID_HANDLE)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.InvalidParameter" />
        ///                 </term>
        ///                 <description>
        ///                     <paramref name="preferredProtocol" /> is invalid or <see langword="null" />  (SCARD_E_INVALID_PARAMETER)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.InvalidValue" />
        ///                 </term>
        ///                 <description>Invalid sharing mode, requested protocol, or reader name (SCARD_E_INVALID_VALUE)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.NoService" />
        ///                 </term>
        ///                 <description>The server is not runing (SCARD_E_NO_SERVICE)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.NoSmartcard" />
        ///                 </term>
        ///                 <description>No smart card present (SCARD_E_NO_SMARTCARD)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.NotReady" />
        ///                 </term>
        ///                 <description>Could not allocate the desired port (SCARD_E_NOT_READY)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.ProtocolMismatch" />
        ///                 </term>
        ///                 <description>Requested protocol is unknown (SCARD_E_PROTO_MISMATCH)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.ReaderUnavailable" />
        ///                 </term>
        ///                 <description>Could not power up the reader or card (SCARD_E_READER_UNAVAILABLE)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.SharingViolation" />
        ///                 </term>
        ///                 <description>Someone else has exclusive rights (SCARD_E_SHARING_VIOLATION)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.UnknownReader" />
        ///                 </term>
        ///                 <description>The reader name is <see langword="null" /> (SCARD_E_UNKNOWN_READER)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.UnsupportedFeature" />
        ///                 </term>
        ///                 <description>Protocol not supported (SCARD_E_UNSUPPORTED_FEATURE)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.CommunicationError" />
        ///                 </term>
        ///                 <description>An internal communications error has been detected (SCARD_F_COMM_ERROR)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.InternalError" />
        ///                 </term>
        ///                 <description>An internal consistency check failed (SCARD_F_INTERNAL_ERROR)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.UnpoweredCard" />
        ///                 </term>
        ///                 <description>Card is not powered (SCARD_W_UNPOWERED_CARD)</description>
        ///             </item>
        ///             <item>
        ///                 <term>
        ///                     <see cref="F:PCSC.SCardError.UnresponsiveCard" />
        ///                 </term>
        ///                 <description>Card is mute (SCARD_W_UNRESPONSIVE_CARD)</description>
        ///             </item>
        ///         </list>
        ///     </para>
        /// </returns>
        /// <remarks>
        ///     <para>
        ///         <paramref name="preferredProtocol" />  is a bit mask of acceptable protocols for the connection. You can use (<see cref="F:PCSC.SCardProtocol.T0" /> | <see cref="F:PCSC.SCardProtocol.T1" />) if you do not have a preferred protocol. The protocol used with this connection will be stored in <see cref="P:PCSC.ISCardReader.ActiveProtocol" />.</para>
        ///     <para>This method calls the API function SCardConnect().</para>
        ///     <example>
        ///         <code lang="C#">
        /// // Establish PC/SC context.
        /// SCardContext ctx = new SCardContext();
        /// ctx.Establish(SCardScope.System);
        /// 
        /// // Create a Smart Card reader object and connect to it.
        /// ISCardReader reader = new SCardReader(ctx);
        /// SCardError serr = reader.Connect("OMNIKEY CardMan 5x21 00 00",
        /// 	SCardShareMode.Shared,
        /// 	SCardProtocol.Any);
        ///   </code>
        ///     </example>
        /// </remarks>
        public SCardError Connect(string readerName, SCardShareMode mode, SCardProtocol preferredProtocol)
        {
            if (readerName == null) {
                throw new ArgumentNullException("readerName");
            }

            if (readerName.IsNullOrWhiteSpace()) {
                throw new UnknownReaderException(SCardError.InvalidValue, "Invalid card reader name.");
            }

            if (_context == null || _context.Handle.Equals(IntPtr.Zero)) {
                throw new InvalidContextException(SCardError.InvalidHandle, "Invalid connection context.");
            }

            IntPtr hCard;
            SCardProtocol dwActiveProtocol;

            var rc = Platform.Lib.Connect(_context.Handle,
                readerName,
                mode,
                preferredProtocol,
                out hCard,
                out dwActiveProtocol);

            if (rc != SCardError.Success) {
                return rc;
            }

            _cardHandle = hCard;
            _activeprot = dwActiveProtocol;
            _readername = readerName;
            _sharemode = mode;

            return rc;
        }
Beispiel #38
0
        /// <summary>Connects the specified reader.</summary>
        /// <param name="readerName">Name of the reader.</param>
        /// <param name="mode">The share mode.</param>
        /// <param name="protocol">The communication protocol. <seealso cref="ISCardReader.Connect(string,SCardShareMode,SCardProtocol)" /></param>
        public virtual void Connect(string readerName, SCardShareMode mode, SCardProtocol protocol) {
            if (readerName == null) {
                throw new ArgumentNullException("readerName");
            }

            if (protocol == SCardProtocol.Unset) {
                throw new InvalidProtocolException(SCardError.InvalidValue);
            }

            if (((long) mode) == 0) {
                throw new InvalidShareModeException(SCardError.InvalidValue);
            }

            var sc = _reader.Connect(readerName, mode, protocol);

            // Throws an exception if sc != SCardError.Success
	        sc.ThrowIfNotSuccess();
        }
Beispiel #39
0
        public SCardError Connect(string name,
                                  SCardShareMode mode,
                                  SCardProtocol prefProto)
        {
            if (name == null ||
                name.Equals(""))
                throw new UnknownReaderException(SCardError.InvalidValue, "Invalid card reader name.");

            if (context == null || context.contextPtr.Equals(IntPtr.Zero))
                throw new InvalidContextException(SCardError.InvalidHandle, "Invalid connection context.");

            SCardError rc;
            IntPtr hCard = IntPtr.Zero;
            SCardProtocol dwActiveProtocol;

            rc = SCardAPI.Lib.Connect(context.contextPtr,
                              name,
                              mode,
                              prefProto,
                              out hCard,
                              out dwActiveProtocol);

            if (rc == SCardError.Success)
            {
                cardHandle = hCard;
                activeprot = dwActiveProtocol;
                readername = name;
                sharemode = mode;
            }

            return rc;
        }
Beispiel #40
0
 /// <summary>Initializes a new instance of the <see cref="IsoReader" /> class and immediately connects to the reader.</summary>
 /// <param name="reader">The supplied reader will be used for communication with the smart card.</param>
 /// <param name="readerName">Name of the reader to connect with.</param>
 /// <param name="mode">The share mode.</param>
 /// <param name="protocol">The communication protocol. <seealso cref="ISCardReader.Connect(string,SCardShareMode,SCardProtocol)" /></param>
 /// <param name="disconnectReaderOnDispose">if set to <c>true</c> the supplied <paramref name="reader" /> will be disconnected on <see cref="Dispose()" />.</param>
 public IsoReader(ISCardReader reader, string readerName, SCardShareMode mode, SCardProtocol protocol,
     bool disconnectReaderOnDispose = true)
     : this(reader, disconnectReaderOnDispose) {
     Connect(readerName, mode, protocol);
 }
Beispiel #41
0
        public SCardError Reconnect(SCardShareMode mode,
                                   SCardProtocol prefProto,
                                   SCardReaderDisposition initExec)
        {
            if (cardHandle.Equals(IntPtr.Zero))
                throw new InvalidOperationException(
                    "Reader is currently not connected or no card handle has been returned.");

            SCardProtocol dwActiveProtocol = activeprot;

            SCardError rc = SCardAPI.Lib.Reconnect(cardHandle,
                                         mode,
                                         prefProto,
                                         initExec,
                                         out dwActiveProtocol);

            if (rc == SCardError.Success)
            {
                activeprot = dwActiveProtocol;
                sharemode = mode;
            }

            return rc;
        }
Beispiel #42
0
 private static extern SCardError SCardReconnect(IntPtr hCard, SCardShareMode dwShareMode, SCardProtocol dwPreferredProtocols, SCardDisposition dwDisposition, [Out] out SCardProtocol pdwActiveProtocol);