如何解决SEHException,VB .NET 2010

时间:2012-12-22 16:19:24

标签: vb.net

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

有什么想法吗?感谢

1 个答案:

答案 0 :(得分:1)

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