第一次尝试无法访问智能卡,其他都成功

Cannot access Smart Card at first attempt, any other is successful

我在使用 PCSC Reader 和智能卡时遇到了问题。我无法使用我的 GUI 应用程序访问该卡。它在控制台示例应用程序中就像一个魅力。
我收到异常:

SCard.Connect Error 0x8010000B: The smart card cannot be accessed because of other connections outstanding!
A first chance exception of type 'GS.SCard.WinSCardException' occurred in GS.CSharpPCSC.dll

但是当我取出卡并重新插入时,它工作正常。
我认为卡在插入时被我的 Windows 机器中的其他进程访问,所以我创建了一个 whileret 值等于-2146435061WinSCardExceptioncontinue 循环中,或 break 如果连接正常。
我连接卡的步骤:

PCSCReader reader = new PCSCReader();
string[] readers = reader.SCard.ListReaders(); 
// Returns 3 readers (even though I have 2 connected, but when I once connected the third one it now appears always) - why?
// Here with GUI I choose interested reader (which is really connected)
reader.SCard.ReleaseContext();
reader.Disconnect(); // In case there is any reader connected
// Here I stop my worker so that It will not try to access reader when it is not connected
reader.Connect(readers[1]); // For example let's connect to reader 1
// Now the worker starts working

//...DoWork method of worker:
while(true)
{
  try {reader.ActivateCard(); break;} // break if successfully connected
  // If the ex status is positive then there is some other issue which is handled by bigger try-catch, but for case ret is -2146435061 i want to continue the loop
  catch (WinSCardException ex) {if (ex.Status > -100) throw (ex); }
  // But this throw Exception over and over again

需要帮助。
我使用这个包装器:http://www.smartcard-magic.net/en/pc-sc-reader/csharppcsc-wrapper/
示例程序看起来几乎相同,但不会抛出任何错误。

using System;
using System.Diagnostics;
using GS.Apdu;
using GS.PCSC;
using GS.SCard;
using GS.SCard.Const;
using GS.Util.Hex;

namespace ExamplePCSCReader
{
    class Program
    {
        static void Main( string[] args )
        {
            ConsoleTraceListener consoleTraceListener = new ConsoleTraceListener();
            Trace.Listeners.Add(consoleTraceListener);

            PCSCReader reader = new PCSCReader();

            try
            {
                reader.Connect();
                reader.ActivateCard();

                RespApdu respApdu = reader.Exchange("00 B0 00 00 0A"); // Get Card UID ...
                if (respApdu.SW1SW2 == 0x9000)
                {
                    Console.WriteLine("ICCID  = 0x" + HexFormatting.ToHexString(respApdu.Data, true));
                }
            }
            catch (WinSCardException ex)
            {
                Console.WriteLine( ex.WinSCardFunctionName + " Error 0x" + 
                                   ex.Status.ToString( "X08" ) + ": " + ex.Message );
            }
            catch (Exception ex)
            {
                Console.WriteLine( ex.Message );
            }
            finally
            {
                reader.Disconnect();
                Console.WriteLine( "Please press any key..." );
                Console.ReadLine();
            }
        }
    }
}
}

关于访问 reader 的问题 - 解决方案是使用命令共享 reader:

reader.ActivateCard(GS.SCard.Const.SCARD_SHARE_MODE.Shared, GS.SCard.Const.SCARD_PROTOCOL.Tx);

列出 not 存在的 readers 的问题是因为如果未建立上下文,包装器将从 Windows 注册表。当我在列出 readers 之前建立上下文时 - 只出现连接的 readers。

reader.SCard.EstablishContext();
readers = reader.SCard.ListReaders();

我和你有同样的问题...但我找到了解决方案。

从 SCardShareMode.Exclusive 更改为 > SCardShareMode.Shared

            _hContext = new SCardContext();
            _hContext.Establish(SCardScope.System);

            // Create a _reader object using the existing context
            _reader = new SCardReader(_hContext);

            // Connect to the card
            if (readerName == null || readerName == String.Empty)
            {
                // Retrieve the list of Smartcard _readers
                string[] szReaders = _hContext.GetReaders();
                if (szReaders.Length <= 0)
                    throw new PCSCException(SCardError.NoReadersAvailable,
                        "Could not find any Smartcard _reader.");

                _err = _reader.Connect(szReaders[0],
                            SCardShareMode.Shared,
                            SCardProtocol.T0 | SCardProtocol.T1);
                CheckErr(_err);
             }

我知道这个主题很老了,但它可能会帮助将来遇到这个问题的其他人。共享 reader 是一种解决方法,而不是解决方法。事实上,由于其他未完成的连接,无法以独占模式连接这一事实意味着 windows 进程正在您的应用程序之外的智能卡上执行操作。 这可能会导致您的应用程序访问智能卡时出现问题,尤其是在同时使用多个 readers/cards 时。

真正的解决方法是在 windows 机器上工作时更改组策略编辑器中的一些配置。它应该是这样的:

然后 reader 应该能够以独占或直接模式连接到卡

干杯