问题描述
我设置pkcs11interop库如下
Pkcs11Library = PkcsFactories.Pkcs11LibraryFactory.LoadPkcs11Library(PkcsFactories,pkcs11LibraryPath,AppType.MultiThreaded)
然后我立即使用包含 WaitForSlotEvent 的 WPF dispatcherTimer
Private Sub CardInsertedTimer_Tick(sender As Object,e As EventArgs)
Dim eventHappened As Boolean
Dim id As ULong
Pkcs11Library.WaitForSlotEvent(WaitType.NonBlocking,eventHappened,id)
If eventHappened Then
Debug.Print("CardInsertedEvent - Event - Event: " + eventHappened.ToString + " ID: " + id.ToString)
End If
End Sub
我希望这里没有惊喜。如果插槽(它是一个 USB 智能卡读卡器)被移除,那么我会收到一个 C_WaitForSlotEvent 返回的 CKR_DEVICE_ERROR。
- 一个 dispatcherTimer 可以用于 WaitForSlotEvent 吗?
- 我应该怎么做才能避免 C_WaitForSlotEvent CKR_DEVICE_ERROR?
- pkcs11interop 是否处理插槽的添加和删除(即 智能卡读卡器)?
- 如果 pkcs11interop 不处理添加和删除插槽正在轮询 唯一的方法以及最好的投票方式是什么?
解决方法
> DispatcherTimer 是否可以用于 WaitForSlotEvent?
一般应该没问题。您也可以考虑在从单独线程调用的阻塞模式下使用 WaitForSlotEvent
。
>我应该怎么做才能避免 C_WaitForSlotEvent CKR_DEVICE_ERROR?
您需要询问非托管 PKCS#11 库的供应商。
AFAIK C_WaitForSlotEvent
是为令牌/卡相关事件而不是插槽/读卡器相关事件设计的。 PKCS#11 specification 指出,当您添加或删除插槽/读取器时,您可能需要重新加载 PKCS#11 库甚至重新启动操作系统:
在某些平台或较早的 PKCS11 兼容库上,可能需要成功调用 C_Initialize
或重新启动整个系统。
> pkcs11Interop 是否处理插槽(即智能卡读卡器)的添加和删除?
Pkcs11Interop 除了让您访问 PKCS#11 specification 中描述的非托管函数 C_WaitForSlotEvent
外,什么都不做。
> 如果 pkcs11Interop 不处理添加和删除插槽,那么轮询是唯一的方式,最好的轮询方式是什么?
查看第一个问题的答案。