private void CheckResult(SmartCardErrors result)
 {
     if (result != SmartCardErrors.SCARD_S_SUCCESS)
     {
         ;
     }
     //throw new Win32Exception((int)result);
 }
        public WindowsSmartCardManager()
        {
            // Establish the Windows Smart Card context
            SmartCardErrors result = EstablishContext(ScopeOptions.User, IntPtr.Zero, IntPtr.Zero, ref context);

            CheckResult(result);

            // List all readers
            int size = 2048;

            readers = new Collection <string>();
            // Ask for the size of the buffer first.
            if (ListReaders(context, null, null, ref size) == SmartCardErrors.SCARD_S_SUCCESS)
            {
                // Allocate a string of the proper size.
                string names = new string(' ', size);
                if (ListReaders(context, null, names, ref size) == SmartCardErrors.SCARD_S_SUCCESS)
                {
                    // The 'names' string will contain a multi-string of the,
                    // reader names i.e. they are separated by 0x00 characters.
                    string name = string.Empty;
                    for (int i = 0; i < names.Length; i++)
                    {
                        if (names[i] == 0x00)
                        {
                            if (name.Length > 0)
                            {
                                //
                                // We have the reader name.
                                //
                                readers.Add(name);
                                name = string.Empty;
                            }
                        }
                        else
                        {
                            // Append found character.
                            name = name + new string(names[i], 1);
                        }
                    }
                }
            }
            states = new ReaderState[readers.Count];
            for (int i = 0; i < readers.Count; i++)
            {
                states[i].Reader = readers[i];
            }

            if (readers.Count > 0)
            {
                worker = new BackgroundWorker();
                worker.WorkerSupportsCancellation = true;
                worker.DoWork += new DoWorkEventHandler(WaitChangeStatus);
                worker.RunWorkerAsync();
            }
        }
        public void Dispose()
        {
            worker.CancelAsync();

            // Obtaina lock when we use the context pointer, which may be used (is used every 1s!) by
            // WaitChangeStatus.
            lock (this) {
                if (context != IntPtr.Zero)
                {
                    SmartCardErrors result = ReleaseContext(context);
                    CheckResult(result);
                    context = IntPtr.Zero;
                }
            }
        }
        public void Dispose()
        {
            try{
                worker.CancelAsync();

                // Obtaina lock when we use the context pointer, which may be used (is used every 1s!) by
                // WaitChangeStatus.
                lock (this) {
                    if (context != IntPtr.Zero)
                    {
                        SmartCardErrors result = ReleaseContext(context);
                        CheckResult(result);
                        context = IntPtr.Zero;
                    }
                }
            }
            catch {
                //This is to catch an error message that many users were getting on shutdown of the program
            }
        }
Example #5
0
 internal PcscException(SmartCardErrors error) : this((int)error)
 {
 }
Example #6
0
		private void CheckResult(SmartCardErrors result) {
			//if(result != SmartCardErrors.SCARD_S_SUCCESS);
				//throw new Win32Exception((int)result);
		}
Example #7
0
        internal static Exception ToException(int returnValue)
        {
            PcscException ex = new PcscException(returnValue);

            switch (returnValue)
            {
            case 109:
                return(new NotSupportedException("The local system does not support smart card redirection", ex));

            default:
            {
                SmartCardErrors value = (SmartCardErrors)returnValue;
                switch (value)
                {
                case SmartCardErrors.InternalError:
                    return(new SystemException("PC/SC internal error", ex));

                case SmartCardErrors.Cancelled:
                    return(new OperationCanceledException("Cancelled", ex));

                case SmartCardErrors.InvalidHandle:
                    return(new ArgumentException("Invalid handle", ex));

                case SmartCardErrors.InvalidParameter:
                    return(new ArgumentException("Invalid parameter", ex));

                case SmartCardErrors.InvalidTarget:
                    return(new SystemException("Invalid target", ex));

                case SmartCardErrors.NoMemory:
                    return(new OutOfMemoryException("No memory", ex));

                case SmartCardErrors.WaitedTooLong:
                    return(new TimeoutException("Waited too long", ex));

                case SmartCardErrors.InsufficientBuffer:
                    return(new InternalBufferOverflowException("Insufficient buffer", ex));

                case SmartCardErrors.UnknownReader:
                    return(new ArgumentException("Unknown reader", ex));

                case SmartCardErrors.Timeout:
                    return(new TimeoutException("Timeout", ex));

                case SmartCardErrors.SharingViolation:
                    return(new SmartCardSharingException(ex));

                case SmartCardErrors.NoSmartcard:
                    return(new Exception("No smart card", ex));

                case SmartCardErrors.UnknownCard:
                    return(new Exception("Unknown card", ex));

                case SmartCardErrors.CantDispose:
                    return(new Exception("Can't dispose", ex));

                case SmartCardErrors.ProtocolMismatch:
                    return(new IOException("Protocol mismatch", ex));

                case SmartCardErrors.NotReady:
                    return(new InvalidOperationException("Not ready", ex));

                case SmartCardErrors.InvalidValue:
                    return(new ArgumentException("Invalid value", ex));

                case SmartCardErrors.SystemCancelled:
                    return(new Exception("System cancelled", ex));

                case SmartCardErrors.CommError:
                    return(new IOException("Comm error", ex));

                case SmartCardErrors.UnknownError:
                    return(new Exception("Unknown error", ex));

                case SmartCardErrors.InvalidAtr:
                    return(new ArgumentException("Invalid ATR", ex));

                case SmartCardErrors.NotTransacted:
                    return(new Exception("Not transacted", ex));

                case SmartCardErrors.ReaderUnavailable:
                    return(new Exception("Reader unavailable", ex));

                case SmartCardErrors.Shutdown:
                    return(new SystemException("Shutdown", ex));

                case SmartCardErrors.PciTooSmall:
                    return(new SystemException("PCI too small", ex));

                case SmartCardErrors.ReaderUnsupported:
                    return(new NotSupportedException("Reader unsupported", ex));

                case SmartCardErrors.DuplicateReader:
                    return(new ArgumentException("Duplicate reader", ex));

                case SmartCardErrors.CardUnsupported:
                    return(new NotSupportedException("Card unsupported", ex));

                case SmartCardErrors.NoService:
                    return(new SystemException("No service", ex));

                case SmartCardErrors.ServiceStopped:
                    return(new SystemException("Service stopped", ex));

                case SmartCardErrors.Unexpected:
                    return(new Exception("Unexpected", ex));

                case SmartCardErrors.IccInstallation:
                    return(new Exception("ICC installation", ex));

                case SmartCardErrors.IccCreateOrder:
                    return(new Exception("ICC create order", ex));

                case SmartCardErrors.UnsupportedFeature:
                    return(new NotSupportedException("Unsupported feature", ex));

                case SmartCardErrors.DirNotFound:
                    return(new DirectoryNotFoundException("Directory not found", ex));

                case SmartCardErrors.FileNotFound:
                    return(new FileNotFoundException("File not found", ex));

                case SmartCardErrors.NoDir:
                    return(new InvalidOperationException("No directory", ex));

                case SmartCardErrors.NoFile:
                    return(new InvalidOperationException("No file", ex));

                case SmartCardErrors.NoAccess:
                    return(new Exception("No access", ex));

                case SmartCardErrors.WriteTooMany:
                    return(new Exception("Write too many", ex));

                case SmartCardErrors.BadSeek:
                    return(new Exception("Bad seek", ex));

                case SmartCardErrors.InvalidChv:
                    return(new Exception("Invalid CHV", ex));

                case SmartCardErrors.UnknownResMng:
                    return(new ArgumentException("Unknown resource manager", ex));

                case SmartCardErrors.NoSuchCertificate:
                    return(new Exception("No such certificate", ex));

                case SmartCardErrors.CertificateUnavailable:
                    return(new Exception("Certificate unavailable", ex));

                case SmartCardErrors.NoReadersAvailable:
                    return(new Exception("No readers available", ex));

                case SmartCardErrors.CommDataLost:
                    return(new IOException("Comm data lost", ex));

                case SmartCardErrors.NoKeyContainer:
                    return(new Exception("No key container", ex));

                case SmartCardErrors.ServerTooBusy:
                    return(new SystemException("Server too busy", ex));

                case SmartCardErrors.UnsupportedCard:
                    return(new NotSupportedException("Unsupported card", ex));

                case SmartCardErrors.UnresponsiveCard:
                    return(new IOException("UnresponsiveCard", ex));

                case SmartCardErrors.UnpoweredCard:
                    return(new IOException("Unpowered card", ex));

                case SmartCardErrors.ResetCard:
                    return(new IOException("Reset card", ex));

                case SmartCardErrors.RemovedCard:
                    return(new IOException("Removed card", ex));

                case SmartCardErrors.SecurityViolation:
                    return(new System.Security.SecurityException("Security violation", ex));

                case SmartCardErrors.WrongChv:
                    return(new Exception("Wrong CHV", ex));

                case SmartCardErrors.ChvBlocked:
                    return(new Exception("CHV blocked", ex));

                case SmartCardErrors.Eof:
                    return(new IOException("EOF", ex));

                case SmartCardErrors.CancelledByUser:
                    return(new Exception("Cancelled by user", ex));

                case SmartCardErrors.CardNotAuthenticated:
                    return(new Exception("Card not authenticated", ex));

                default:
                    return(new Exception("PC/SC error " + value.ToString() + " (" + returnValue.ToString("X") + ")", ex));
                }
            }
            }
        }
Example #8
0
		internal PcscException(SmartCardErrors error) : this((int)error) {
		}