我在使用 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 机器中的其他进程可以访问该卡,所以我创建了一个当值等于in和循环或连接正常while
时特别捕获的情况。
我连接到卡的步骤:ret
-2146435061
WinSCardException
continue
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/cshappcsc-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();
}
}
}
}
}