2
System.Runtime.InteropServices.SEHException was unhandled
  Message=External component has thrown an exception.
  Source=mscorlib
  ErrorCode=-2147467259
  StackTrace:
       at Microsoft.Win32.Win32Native.CloseHandle(IntPtr handle)
       at Microsoft.Win32.SafeHandles.SafeFileHandle.ReleaseHandle()
       at System.Runtime.InteropServices.SafeHandle.InternalFinalize()
       at System.Runtime.InteropServices.SafeHandle.Dispose(Boolean disposing)
       at System.Runtime.InteropServices.SafeHandle.Finalize()
  InnerException: 

我阅读了所有关于 SEHException 的帖子,但我无法解决我的问题,请帮忙。这是我怀疑的代码:

If hWinUSBInterface = INVALID_HANDLE_VALUE And hDevice = INVALID_HANDLE_VALUE Then

    If Not tmrAutoConnect.Enabled Then
        RaiseEvent Notify(2, "Not connected")
    End If
    Return
End If

Try
    If hWinUSBInterface <> INVALID_HANDLE_VALUE Then
        WinUsb_Free(hWinUSBInterface)
        hWinUSBInterface = INVALID_HANDLE_VALUE
    End If

    If hDevice <> INVALID_HANDLE_VALUE Then

        If CloseHandle(hDevice) Then
            hDevice = INVALID_HANDLE_VALUE
            RaiseEvent Disconnected()
        Else
            Dim ErrorStatus As Integer = Err.LastDllError
            RaiseEvent Error(1, ErrorStatus, "Disconnect")
        End If

    End If
Catch ex As Exception

End Try

任何想法?谢谢

4

1 回答 1

1

您似乎在 SafeFileHandle hDevice 上使用 kernel32.dll CloseHandle。垃圾收集器不知道这些 CloseHandle 调用,并且在尝试清理 SafeFileHandle 时会生成此错误。尝试使用 hDevice.Close() 代替。

于 2012-12-25T10:38:18.440 回答