如何解决SEHException,VB.NET2010

如何解决SEHException,VB.NET2010,vb.net,Vb.net,我阅读了所有关于SEHException的帖子,但我无法解决我的问题,请帮助。以下是我怀疑的代码: System.Runtime.InteropServices.SEHException was unhandled Message=External component has thrown an exception. Source=mscorlib ErrorCode=-2147467259 StackTrace: at Microsoft.Win32.Win32N

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

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: 
如果hwinusinterface=INVALID\u HANDLE\u值和hDevice=INVALID\u HANDLE\u值,则
如果未启用tmrAutoConnect.则
RaiseEvent通知(2,“未连接”)
如果结束
返回
如果结束
尝试
如果HwinusInterface的句柄值无效,则
WinUsb_免费(硬件接口)
HwinusInterface=无效的句柄值
如果结束
如果hDevice的句柄值无效,则
如果关闭手柄(hDevice),则
hDevice=无效的句柄值
RaiseEvent断开连接()
其他的
Dim ErrorStatus为整数=Err.LastDllError
RaiseEvent错误(1,错误状态,“断开”)
如果结束
如果结束
特例
结束尝试

有什么想法吗?感谢您在SafeFileHandle hDevice上使用了kernel32.dll CloseHandle。垃圾收集器不知道这些CloseHandle调用,并在尝试清理SafeFileHandle时生成此错误。尝试改用hDevice.Close()。

此代码不会引发异常。您可能正在使用接受IntPtr的FileStream构造函数。然后不要调用CloseHandle(),FileStream的终结器已经这样做了。
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