我已经将这个软件在生产环境中运行多年,之前从未见过这个问题。我刚收到一台新笔记本电脑(HP EliteBook 8470p),它带有内置的Alcor Micro USB智能卡读取器。
以下代码将列出系统上的所有读卡器,并且似乎工作正常。我们的一些系统将有3或4个读卡器插入单个计算机。它已经通过了十几个型号的测试,没有任何问题。
奇怪的是,只有在插入智能卡时,Alcor读卡器才会被列出。如果我在设备管理器中查看它,在“智能卡读卡器”下不会显示它,除非插入卡片(除非我进入“查看”>“显示隐藏的设备”)。
有人知道为什么会这样,或者是否有办法确保它在我的软件中被列出?
谢谢。
代码:
以下代码将列出系统上的所有读卡器,并且似乎工作正常。我们的一些系统将有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读卡器。
谢谢!