我已经让这个软件在生产环境中运行多年,以前从未见过这个问题。我刚收到一台内置Alcor Micro USB 智能卡读卡器的新笔记本电脑 (HP EliteBook 8470p) 。
下面的代码将列出系统上的所有阅读器,并且似乎工作正常。我们的一些系统将有 3 或 4 个读卡器插入一台计算机。它已经过十几个模型的测试,没有任何问题。
奇怪的是,Alcor 读卡器只有在插入智能卡时才会被列出。如果我在设备管理器中查看它,在插入卡之前它不会显示在“智能卡读卡器”下(除非我去查看>显示隐藏的设备)。
有谁知道这是为什么,或者是否有办法确保它在我的软件中列出?
谢谢你。
编码:
[DllImport("WINSCARD.DLL", EntryPoint = "SCardEstablishContext", CharSet = CharSet.Unicode, SetLastError = true)]
static internal extern uint EstablishContext(ScopeOption scope, IntPtr reserved1,
IntPtr reserved2, ref SmartcardContextSafeHandle context);
[DllImport("WINSCARD.DLL", EntryPoint = "SCardListReaders", CharSet = CharSet.Unicode, SetLastError = true)]
static internal extern uint ListReaders(SmartcardContextSafeHandle context, string groups,
string readers, ref int size);
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);
}
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;
}
顺便说一句,这段代码是由我猜测的其他人编写的(由于评论过多)在网上的其他地方找到了它。我对它有些熟悉,但从未深入过它。我已经尝试对其进行一些调整,但根本无法让它列出该 Alcor 阅读器。
谢谢!