删除 reader 时 WaitForSlotEvent 的 pkcs11Interop 问题

pkcs11Interop problem with WaitForSlotEvent when reader is removed

我设置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 智能卡 reader)被移除,那么我会得到 C_WaitForSlotEvent 返回 CKR_DEVICE_ERROR。

> DispatcherTimer 可以用于 WaitForSlotEvent 吗?

总体来说应该没问题。您还可以考虑在从单独线程调用的阻塞模式下使用 WaitForSlotEvent

> 我应该怎么做才能避免C_WaitForSlotEvent CKR_DEVICE_ERROR?

您需要咨询非托管 PKCS#11 库的供应商。

AFAIK C_WaitForSlotEvent 是为 token/card 相关事件设计的,而不是为 slot/reader 相关事件设计的。 PKCS#11 specification 声明当您添加或删除 slot/reader 时,您可能需要重新加载 PKCS#11 库甚至重新启动 OS:

On some platforms, or earlier PKCS11 compliant libraries, it may be necessary to successfully call C_Initialize or to restart the entire system.

> pkcs11Interop 是否处理插槽(即智能卡读卡器)的添加和删除?

Pkcs11Interop 除了允许您访问 PKCS#11 specification.

中描述的非托管函数 C_WaitForSlotEvent 之外,什么都不做

> 如果 pkcs11Interop 不处理添加和删除插槽是唯一的轮询方式,什么是最好的轮询方式?

查看第一个问题的答案。