//We should not provide a finalizer. SafeHandle's critical
        //finalizer will call ReleaseHandle for us.
        protected override bool ReleaseHandle()
        {
            SmartcardErrorCode result =
                (SmartcardErrorCode)UnsafeNativeMethods.ReleaseContext(handle);

            return(result == SmartcardErrorCode.None);
        }
Ejemplo n.º 2
0
 private bool EstablishContext()
 {
     if ((this.HasContext))
     {
         return(true);
     }
     this._lastErrorCode =
         (SmartcardErrorCode)UnsafeNativeMethods.EstablishContext(ScopeOption.System,
                                                                  IntPtr.Zero, IntPtr.Zero, ref this._context);
     return(this._lastErrorCode == SmartcardErrorCode.None);
 }
Ejemplo n.º 3
0
        private int GetReaderListBufferSize()
        {
            if ((this._context.IsInvalid))
            {
                return(0);
            }
            int result = 0;

            this._lastErrorCode =
                (SmartcardErrorCode)UnsafeNativeMethods.ListReaders(
                    this._context, null, null, ref result);
            return(result);
        }
Ejemplo n.º 4
0
        public ArrayList ListReaders()
        {
            ArrayList result = new ArrayList();

            //Make sure a context has been established before
            //retrieving the list of smartcard readers.
            if (this.EstablishContext())
            {
                //Ask for the size of the buffer first.
                int size = this.GetReaderListBufferSize();

                //Allocate a string of the proper size in which
                //to store the list of smartcard readers.
                string readerList = new string('\0', size);
                //Retrieve the list of smartcard readers.
                this._lastErrorCode =
                    (SmartcardErrorCode)UnsafeNativeMethods.ListReaders(this._context,
                                                                        null, readerList, ref size);
                if ((this._lastErrorCode == SmartcardErrorCode.None))
                {
                    //Extract each reader from the returned list.
                    //The readerList string will contain a multi-string of
                    //the reader names, i.e. they are seperated by 0x00
                    //characters.
                    string readerName = string.Empty;
                    for (int i = 0; i <= readerList.Length - 1; i++)
                    {
                        if ((readerList[i] == '\0'))
                        {
                            if ((readerName.Length > 0))
                            {
                                //We have a smartcard reader's name.
                                result.Add(readerName);
                                readerName = string.Empty;
                            }
                        }
                        else
                        {
                            //Append the found character.
                            readerName += new string(readerList[i], 1);
                        }
                    }
                }
            }
            return(result);
        }