Esempio n. 1
0
 public void Dispose()
 {
     if (this.Context != IntPtr.Zero)
     {
         SCardApi.SCardReleaseContext(this.Context);
     }
 }
Esempio n. 2
0
        public byte[] Transmit(IntPtr hCard, byte[] pbSendBuffer)
        {
            Trace.Assert(SCardApi.sessionProtocols.ContainsKey(hCard));
            IntPtr SendPci              = IntPtr.Zero;
            IntPtr ResponsePci          = IntPtr.Zero;
            uint   ResponseBufferLength = 512;

            byte[] ResponseBuffer = new byte[ResponseBufferLength];
            switch ((SCardProtocol)SCardApi.sessionProtocols[hCard])
            {
            case SCardProtocol.T0:
                SendPci = SCardApi.SCARD_PCI_T0;
                break;

            case SCardProtocol.T1:
                SendPci = SCardApi.SCARD_PCI_T1;
                break;

            case SCardProtocol.Raw:
                SendPci = SCardApi.SCARD_PCI_RAW;
                break;
            }

            SCardError Error = SCardApi.SCardTransmit(hCard, SendPci, pbSendBuffer, (uint)pbSendBuffer.Length, ResponsePci, ResponseBuffer, ref ResponseBufferLength);

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

            byte[] Response = new byte[ResponseBufferLength];
            Array.Copy(ResponseBuffer, 0, Response, 0, ResponseBufferLength);

            return(Response);
        }
Esempio n. 3
0
        public string[] ListReaders()
        {
            uint CardReadersSize = 0;

            char[] CardReaders = null;

            SCardError Error = SCardApi.SCardListReaders(this.Context, null, null, ref CardReadersSize);

            if (!SCardApi.IsError(Error))
            {
                CardReaders = new char[CardReadersSize];
                Error       = SCardApi.SCardListReaders(this.Context, null, CardReaders, ref CardReadersSize);
            }

            if (SCardApi.IsError(Error))
            {
                switch (Error)
                {
                case SCardError.NoReadersAvailable:
                    return(new string[0]);

                default:
                    throw new SCardException(Error);
                }
            }
            else
            {
                return(SCardApi.ToStringArray(CardReaders));
            }
        }
Esempio n. 4
0
        static SCardApi()
        {
            IntPtr Lib = SCardApi.LoadLibrary("winscard.dll");

            SCardApi.SCARD_PCI_T0  = SCardApi.GetProcAddress(Lib, "g_rgSCardT0Pci");
            SCardApi.SCARD_PCI_T1  = SCardApi.GetProcAddress(Lib, "g_rgSCardT1Pci");
            SCardApi.SCARD_PCI_RAW = SCardApi.GetProcAddress(Lib, "g_rgSCardRawPci");
            SCardApi.FreeLibrary(Lib);
        }
Esempio n. 5
0
        public void Disconnect(IntPtr cardHandle, SCardDisposition dwDisposition)
        {
            SCardError Error = SCardApi.SCardDisconnect(cardHandle, dwDisposition);

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

            SCardApi.sessionProtocols.Remove(cardHandle);
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        public byte[] ControlCardReader(IntPtr hCard, byte[] command)
        {
            byte[]     Response       = new byte[1024];
            int        ResponseLength = 0;
            SCardError Error          = SCardApi.SCardControl(hCard, 0x00312000, command, command.Length, Response, Response.Length, ref ResponseLength);

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

            byte[] RealResponse = new byte[ResponseLength];
            Array.Copy(Response, 0, RealResponse, 0, ResponseLength);

            return(RealResponse);
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
        /// <summary>
        /// Waits for status change on the given list of card readers.
        /// If cancelled by a call to <see cref="Cancel"/> or due to timeout, it returns <c>false</c>, otherwise <c>true</c>
        /// </summary>
        public bool GetStatusChange(int dwTimeout, SCardCardReaderState[] cardReaderStates)
        {
            SCardError Error = SCardApi.SCardGetStatusChange(this.Context, dwTimeout, cardReaderStates, cardReaderStates.Length);

            if (Error == SCardError.Cancelled || Error == SCardError.Timeout)
            {
                return(false);
            }
            else if (SCardApi.IsError(Error))
            {
                throw new SCardException(Error);
            }
            else
            {
                return(true);
            }
        }
Esempio n. 10
0
        public byte[] GetAttribute(IntPtr hCard, SCardAttributes attribute)
        {
            uint       AttributeLength = 0;
            SCardError Error           = SCardApi.SCardGetAttrib(hCard, (uint)attribute, null, ref AttributeLength);

            if (Error == (SCardError)22)
            {
                return(null);
            }
            if (SCardApi.IsError(Error))
            {
                throw new SCardException(Error);
            }

            byte[] Attribute = new byte[AttributeLength];

            Error = SCardApi.SCardGetAttrib(hCard, (uint)attribute, Attribute, ref AttributeLength);
            if (SCardApi.IsError(Error))
            {
                throw new SCardException(Error);
            }

            return(Attribute);
        }
Esempio n. 11
0
 public void Cancel()
 {
     SCardApi.SCardCancel(this.Context);
 }