コード例 #1
0
 internal static string[] _ConvertToStringArray(byte[] array, Encoding decoder)
 {
     if (array != null)
     {
         string tmp = decoder.GetString(array);
         return(SCardHelper._RemoveEmptyStrings(tmp.Split('\0')));
     }
     return(null);
 }
コード例 #2
0
ファイル: SCardReader.cs プロジェクト: uheqiang/p2abcengine
        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));
        }
コード例 #3
0
        /// <inheritdoc />
        public ReaderStatus GetStatus()
        {
            var handle = CardHandle.Handle;

            _api.Status(
                hCard: handle,
                szReaderName: out var readerNames,
                pdwState: out var dwState,
                pdwProtocol: out var dwProtocol,
                pbAtr: out var atr)
            .ThrowIfNotSuccess();

            return(new ReaderStatus(
                       readerNames: readerNames,
                       state: SCardHelper.ToState(dwState),
                       protocol: SCardHelper.ToProto(dwProtocol),
                       atr: atr));
        }
コード例 #4
0
ファイル: SCardContext.cs プロジェクト: RuiVarela/Nespresso
        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));
        }
コード例 #5
0
ファイル: SCardContext.cs プロジェクト: RuiVarela/Nespresso
        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));
        }
コード例 #6
0
ファイル: SCardContext.cs プロジェクト: RuiVarela/Nespresso
        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));
            }
        }
コード例 #7
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));
            }
        }
コード例 #8
0
ファイル: SCardContext.cs プロジェクト: RuiVarela/Nespresso
        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;
        }
コード例 #9
0
ファイル: SCardContext.cs プロジェクト: uheqiang/p2abcengine
        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));
                }
            }
        }
コード例 #10
0
 public InvalidContextException(SCardError serr)
     : base(serr, SCardHelper.StringifyError(serr))
 {
 }
コード例 #11
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;
 }