コード例 #1
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
        public SCardError SetAttrib(IntPtr hCard, IntPtr attributeId, byte[] sendBuffer, int sendBufferLength)
        {
            IntPtr cbAttrLen;

            if (sendBuffer != null)
            {
                if (sendBufferLength > sendBuffer.Length || sendBufferLength < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(sendBufferLength));
                }

                cbAttrLen = (IntPtr)sendBufferLength;
            }
            else
            {
                cbAttrLen = IntPtr.Zero;
            }

            return(SCardHelper.ToSCardError(
                       UnixNativeMethods.SCardSetAttrib(
                           hCard,
                           attributeId,
                           sendBuffer,
                           cbAttrLen)));
        }
コード例 #2
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
        public SCardError ListReaderGroups(IntPtr hContext, out string[] groups)
        {
            var dwGroups = IntPtr.Zero;

            // determine the needed buffer size
            var rc = SCardHelper.ToSCardError(
                UnixNativeMethods.SCardListReaderGroups(
                    hContext,
                    null,
                    ref dwGroups));

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

            // initialize array for returning group names
            var mszGroups = new byte[(int)dwGroups];

            rc = SCardHelper.ToSCardError(
                UnixNativeMethods.SCardListReaderGroups(
                    hContext,
                    mszGroups,
                    ref dwGroups));

            groups = (rc == SCardError.Success)
                ? SCardHelper.ConvertToStringArray(mszGroups, TextEncoding)
                : null;

            return(rc);
        }
コード例 #3
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
        public SCardError GetStatusChange(IntPtr hContext, IntPtr dwTimeout, SCardReaderState[] rgReaderStates)
        {
            SCARD_READERSTATE[] readerstates = null;
            var cReaders = 0;

            if (rgReaderStates != null)
            {
                // copy the last known state into the buffer
                cReaders     = rgReaderStates.Length;
                readerstates = new SCARD_READERSTATE[cReaders];
                for (var i = 0; i < cReaders; i++)
                {
                    readerstates[i] = rgReaderStates[i].UnixReaderState;
                }
            }

            var rc = SCardHelper.ToSCardError(UnixNativeMethods.SCardGetStatusChange(
                                                  hContext,
                                                  dwTimeout,
                                                  readerstates,
                                                  (IntPtr)cReaders));

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

            for (var i = 0; i < cReaders; i++)
            {
                // replace with returned values
                rgReaderStates[i].UnixReaderState = readerstates[i];
            }

            return(rc);
        }
コード例 #4
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
        public SCardError Transmit(IntPtr hCard, IntPtr pioSendPci, byte[] pbSendBuffer, int pcbSendLength,
                                   IntPtr pioRecvPci, byte[] pbRecvBuffer, ref int pcbRecvLength)
        {
            var recvlen = IntPtr.Zero;

            if (pbRecvBuffer != null)
            {
                if (pcbRecvLength > pbRecvBuffer.Length || pcbRecvLength < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(pcbRecvLength));
                }

                recvlen = (IntPtr)pcbRecvLength;
            }
            else
            {
                if (pcbRecvLength != 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(pcbRecvLength));
                }
            }

            var sendbuflen = IntPtr.Zero;

            if (pbSendBuffer != null)
            {
                if (pcbSendLength > pbSendBuffer.Length || pcbSendLength < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(pcbSendLength));
                }

                sendbuflen = (IntPtr)pcbSendLength;
            }
            else
            {
                if (pcbSendLength != 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(pcbSendLength));
                }
            }

            var rc = SCardHelper.ToSCardError(UnixNativeMethods.SCardTransmit(
                                                  hCard,
                                                  pioSendPci,
                                                  pbSendBuffer,
                                                  sendbuflen,
                                                  pioRecvPci,
                                                  pbRecvBuffer,
                                                  ref recvlen));

            pcbRecvLength = (int)recvlen;
            return(rc);
        }
コード例 #5
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
        public SCardError EstablishContext(SCardScope dwScope, IntPtr pvReserved1, IntPtr pvReserved2,
                                           out IntPtr phContext)
        {
            var ctx = IntPtr.Zero;
            var rc  = SCardHelper.ToSCardError(UnixNativeMethods.SCardEstablishContext(
                                                   (IntPtr)dwScope,
                                                   pvReserved1,
                                                   pvReserved2,
                                                   ref ctx));

            phContext = ctx;
            return(rc);
        }
コード例 #6
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
        public SCardError Reconnect(IntPtr hCard, SCardShareMode dwShareMode, SCardProtocol dwPreferredProtocols,
                                    SCardReaderDisposition dwInitialization, out SCardProtocol pdwActiveProtocol)
        {
            var result = UnixNativeMethods.SCardReconnect(
                hCard,
                (IntPtr)dwShareMode,
                (IntPtr)dwPreferredProtocols,
                (IntPtr)dwInitialization,
                out var activeproto);

            pdwActiveProtocol = (SCardProtocol)activeproto;
            return(SCardHelper.ToSCardError(result));
        }
コード例 #7
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
        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 = UnixNativeMethods.SCardConnect(hContext,
                                                        readername,
                                                        (IntPtr)dwShareMode,
                                                        (IntPtr)dwPreferredProtocols,
                                                        out phCard,
                                                        out var activeproto);

            pdwActiveProtocol = (SCardProtocol)activeproto;

            return(SCardHelper.ToSCardError(result));
        }
コード例 #8
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
        public SCardError Control(IntPtr hCard, IntPtr dwControlCode, byte[] pbSendBuffer, int sendBufferLength,
                                  byte[] pbRecvBuffer, int recvBufferLength,
                                  out int lpBytesReturned)
        {
            if (pbSendBuffer == null && sendBufferLength > 0)
            {
                throw new ArgumentException("send buffer is null", nameof(sendBufferLength));
            }

            if ((pbSendBuffer != null && pbSendBuffer.Length < sendBufferLength) || sendBufferLength < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sendBufferLength));
            }

            if (pbRecvBuffer == null && recvBufferLength > 0)
            {
                throw new ArgumentException("receive buffer is null", nameof(recvBufferLength));
            }

            if ((pbRecvBuffer != null && pbRecvBuffer.Length < recvBufferLength) || recvBufferLength < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(recvBufferLength));
            }

            var sendbuflen = (IntPtr)sendBufferLength;
            var recvbuflen = (IntPtr)recvBufferLength;

            var rc = SCardHelper.ToSCardError(UnixNativeMethods.SCardControl(
                                                  hCard,
                                                  dwControlCode,
                                                  pbSendBuffer,
                                                  sendbuflen,
                                                  pbRecvBuffer,
                                                  recvbuflen,
                                                  out var bytesret));

            lpBytesReturned = (int)bytesret;

            return(rc);
        }
コード例 #9
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
        public SCardError GetAttrib(IntPtr hCard, IntPtr attributeId, byte[] receiveBuffer, int receiveBufferLength,
                                    out int attributeLength)
        {
            if (receiveBuffer == null && receiveBufferLength != 0)
            {
                throw new ArgumentOutOfRangeException(nameof(receiveBufferLength));
            }

            if (receiveBuffer != null && (receiveBufferLength < 0 || receiveBufferLength > receiveBuffer.Length))
            {
                throw new ArgumentOutOfRangeException(nameof(receiveBufferLength));
            }

            var attrlen = (IntPtr)receiveBufferLength;
            var rc      = SCardHelper.ToSCardError(UnixNativeMethods.SCardGetAttrib(
                                                       hCard,
                                                       attributeId,
                                                       receiveBuffer,
                                                       ref attrlen));

            attributeLength = (int)attrlen;
            return(rc);
        }
コード例 #10
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
 public IntPtr GetSymFromLib(string symName)
 {
     return(UnixNativeMethods.GetSymFromLib(symName));
 }
コード例 #11
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
 public SCardError Cancel(IntPtr hContext)
 {
     return(SCardHelper.ToSCardError(UnixNativeMethods.SCardCancel(hContext)));
 }
コード例 #12
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
 public SCardError EndTransaction(IntPtr hCard, SCardReaderDisposition dwDisposition)
 {
     return(SCardHelper.ToSCardError(UnixNativeMethods.SCardEndTransaction(hCard, (IntPtr)dwDisposition)));
 }
コード例 #13
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
 public SCardError BeginTransaction(IntPtr hCard)
 {
     return(SCardHelper.ToSCardError(UnixNativeMethods.SCardBeginTransaction(hCard)));
 }
コード例 #14
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
 public SCardError Disconnect(IntPtr hCard, SCardReaderDisposition dwDisposition)
 {
     return(SCardHelper.ToSCardError(UnixNativeMethods.SCardDisconnect(hCard, (IntPtr)dwDisposition)));
 }
コード例 #15
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
 public SCardError ReleaseContext(IntPtr hContext)
 {
     return(SCardHelper.ToSCardError(UnixNativeMethods.SCardReleaseContext(hContext)));
 }
コード例 #16
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
 public SCardError IsValidContext(IntPtr hContext)
 {
     return(SCardHelper.ToSCardError(UnixNativeMethods.SCardIsValidContext(hContext)));
 }
コード例 #17
0
ファイル: PCSCliteAPI.cs プロジェクト: thachit/pcsc-sharp
        public SCardError Status(IntPtr hCard, out string[] szReaderName, out IntPtr pdwState, out IntPtr pdwProtocol,
                                 out byte[] pbAtr)
        {
            var readerName     = new byte[MAX_READER_NAME * CharSize];
            var readerNameSize = (IntPtr)MAX_READER_NAME;

            pbAtr = new byte[MAX_ATR_SIZE];
            var atrlen = (IntPtr)pbAtr.Length;
            var rc     = SCardHelper.ToSCardError(UnixNativeMethods.SCardStatus(
                                                      hCard,
                                                      readerName,
                                                      ref readerNameSize,
                                                      out var state,
                                                      out var proto,
                                                      pbAtr,
                                                      ref atrlen));

            if (rc == SCardError.InsufficientBuffer || (MAX_READER_NAME < ((int)readerNameSize)) ||
                (pbAtr.Length < (int)atrlen))
            {
                // second try

                if (MAX_READER_NAME < ((int)readerNameSize))
                {
                    // readername byte array was too short
                    readerName = new byte[(int)readerNameSize * CharSize];
                }

                if (pbAtr.Length < (int)atrlen)
                {
                    // ATR byte array was too short
                    pbAtr = new byte[(int)atrlen];
                }

                rc = SCardHelper.ToSCardError(UnixNativeMethods.SCardStatus(
                                                  hCard,
                                                  readerName,
                                                  ref readerNameSize,
                                                  out state,
                                                  out proto,
                                                  pbAtr,
                                                  ref atrlen));
            }

            pdwState    = state;
            pdwProtocol = proto;

            if (rc == SCardError.Success)
            {
                state = state.Mask(STATUS_MASK);
                if ((int)atrlen < pbAtr.Length)
                {
                    Array.Resize(ref pbAtr, (int)atrlen);
                }

                if (((int)readerNameSize) < (readerName.Length / CharSize))
                {
                    Array.Resize(ref readerName, (int)readerNameSize * CharSize);
                }

                szReaderName = SCardHelper.ConvertToStringArray(readerName, TextEncoding);
            }
            else
            {
                szReaderName = null;
            }

            return(rc);
        }