首次尝试无法访问智能卡,其他任何成功

问题描述 投票:0回答:3

我在使用PCSC读卡器和智能卡时遇到麻烦。我无法使用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卡中的其他进程可以访问该卡,因此我创建了一个while,当ret值等于-2146435061中的WinSCardExceptioncontinue循环,如果连接正常,则单击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();
            }
        }
    }
}
}
c# smartcard pcsc gemalto
3个回答
2
投票

关于访问阅读器的问题-解决方案是与命令共享阅读器:

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

列出not现有读者的问题是因为,如果未建立上下文,则包装程序将从Windows Registry中获取读者列表。在列出读者之前建立上下文时-仅显示连接的读者。

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

0
投票

我对您有同样的问题...但是我找到了解决方法。

从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);
             }

0
投票

我知道这个主题很旧,但它可能会帮助将来会遇到此问题的其他人。共享阅读器是一种解决方法,不是解决方案。确实,由于其他出色的连接而无法以独占模式进行连接这一事实意味着Windows进程正在应用程序外部的智能卡上执行操作。这可能会导致您的应用程序访问智能卡时出现问题,特别是如果同时使用多个读卡器/卡,则尤其如此。

真正的解决方法是在Windows计算机上工作时在组策略编辑器中更改某些配置。它看起来应该像这样:enter image description here

读取器随后应能够以独占或直接模式连接到卡

欢呼声

© www.soinside.com 2019 - 2024. All rights reserved.