/// <summary>
        /// Connect the application to the PCSC driver
        /// </summary>
        /// <param name="sReader">The reader name</param>
        /// <param name="nAccessMode">Access mode (Direct, Exclusive or Shared).</param>
        /// <param name="nPreferredProtocols">Protocols the driver should use to communicate with the reader.</param>
        /// <returns>True if successfully connected, False if card removed.</returns>
        public bool Connect(string sReader, SCardAccessMode nAccessMode, SCardProtocolIdentifiers nPreferredProtocols)
        {
            if (this.m_fDisposed)
            {
                throw new ObjectDisposedException(base.GetType().FullName);
            }
            if (((this.m_hCard != IntPtr.Zero) || (this.Context == IntPtr.Zero)) || this.m_fIsConnected)
            {
                throw new InvalidOperationException();
            }

            // Share mode and protocols from caller.
            uint num1 = (uint)nAccessMode;
            uint num2 = (uint)nPreferredProtocols;

            // Establish a connection between application and card docked in the reader.
            // (Gets the handle to the card and the active protocol of the connection)
            uint num3 = WinSCard.SCardConnect(this.Context, sReader, num1, num2, out this.m_hCard, out this.m_dwActiveProtocol);

            // Check error code - either successfully connected or the card was removed are ok.
            switch (num3)
            {
            case (uint)SCardErrorCodes.SCARD_S_SUCCESS:
                //case 0:
                this.m_fIsConnected = true;
                return(true);

            case (uint)SCardErrorCodes.SCARD_W_REMOVED_CARD:
                //case 0x80100069:
                return(false);
            }

            // Any other error codes are raised.
            //SCardException.RaiseWin32ResponseCode(num3);
            throw new InvalidProgramException();
        }
Example #2
0
 internal Card setProtocol(SCardProtocolIdentifiers protocol)
 {
     this.protocol = protocol;
     return(this);
 }
        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);
        }