0

我编写了一个有意的无限循环来识别是否使用 vb.net 在读卡器上检测到或删除了卡。因为我不知道winscard中是否有类似waitForCardPresent的东西。

所以这个概念是,一旦单击按钮Connect,检查卡的 ifinite 循环就会启动。

所以我在 backgroundworker 中的代码如下所示:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Do
        Dim ret As Integer
        Dim rs As WinscardFun.SCARDREADER_STATE
        rs.dwEventState = Nothing
        rs.rgbAtr = Nothing
        rs.pvUserData = Nothing
        rs.cbAtr = Nothing
        rs.szReader = rdr
        rs.dwCurrentState = &H0
        ret = WinscardFun.SCardGetStatusChange(hcontext, -1, rs, 1)
        If ret = 0 And rs.cbAtr > 0 And rs.rgbAtr IsNot Nothing Then

            ReDim atr(rs.cbAtr)
            Array.Copy(rs.rgbAtr, atr, rs.cbAtr)
            If status = "Connected" Then
                'Do nothing
            Else
                test = "Connected"
                Console.Write(test)
            End If

        Else
            If test = "Remove" Then
                'Do nothing
            Else
                status = "Remove"
                Console.Write(test)
            End If
        End If
    Loop

End Sub

此代码说明程序将在后台持续检查卡状态。我写了:

If status = "Connected" Then
                'Do nothing
            Else
                test = "Connected"
                Console.Write(test)
            End If

        Else
            If test = "Remove" Then
                'Do nothing
            Else
                status = "Removed"
                Console.Write(test)
            End If
End If

所以我的程序不会充满状态,只会打印连接删除一次。

所以我的问题是:

  1. javaxx smartcardio 的 waitforcardpresent() 和 waitForCardAbsent() 的概念和我的想法一样吗?
  2. 是否有更好的代码来使用 vb.net/winscard 检测卡状态?如果是,如何?
  3. 我怎样才能停止这个无限循环?假设用户单击“断开连接”按钮?
  4. 我的无限循环不好吗?

编辑 无限循环不是一个好主意。那么如何在不消耗资源的情况下在后台检测卡状态呢?或者我应该如何正确使用scardgetstatuschange?

我虽然认为 -1 在

WinscardFun.SCardGetStatusChange(hcontext, -1, rs, 1)

意味着它将无限循环基于

    ''' <summary>
''' The SCardGetStatusChange function blocks execution until the current availability of the cards in a specific set of readers changes.
''' </summary>
''' <param name="hContext">A handle that identifies the resource manager context. The resource manager context is set by a previous call to the SCardEstablishContext function.</param>
''' <param name="TimeOut">The maximum amount of time, in milliseconds, to wait for an action. A value of zero causes the function to return immediately. A value of INFINITE causes this function never to time out.</param>
''' <param name="ReaderState">
''' An array of SCARD_READERSTATE structures that specify the readers to watch, and that receives the result.
''' To be notified of the arrival of a new smart card reader, set the szReader member of a SCARD_READERSTATE structure to "\\\\?PnP?\\Notification", and set all of the other members of that structure to zero.
''' Important  Each member of each structure in this array must be initialized to zero and then set to specific values as necessary. If this is not done, the function will fail in situations that involve remote card readers.
''' </param>
''' <param name="ReaderCount">The number of elements in the rgReaderStates array.</param>
''' <returns></returns>
<DllImport("winscard.dll", EntryPoint:="SCardGetStatusChangeA", CharSet:=CharSet.Ansi)> _
Public Shared Function SCardGetStatusChange(ByVal hContext As IntPtr, ByVal TimeOut As Integer, ByRef ReaderState As SCARD_READERSTATE, ByVal ReaderCount As Integer) As Integer
End Function
4

0 回答 0