c# 在LibUsbdotnet库进行USB操作时使用GetOverlappedResult()接口异常解决办法

   

在 System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
   在 System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
   在 LibUsbDotNet.Internal.Kernel32.GetOverlappedResult(SafeHandle hDevice, IntPtr lpOverlapped, Int32& lpNumberOfBytesTransferred, Boolean bWait)
   在 LibUsbDotNet.Internal.LibUsb.LibUsbAPI.GetOverlappedResult(SafeHandle interfaceHandle, IntPtr pOverlapped, Int32& numberOfBytesTransferred, Boolean wait) 在 E:\ihorn_mesh_tool\usb_mesh_tools\usb_mesh_tool\LibWinUsb\LibUsb\Internal\LibUsbAPI.cs 中: 第 89 行
   在 LibUsbDotNet.Internal.OverlappedTransferContext.Wait(Int32& transferredCount, Boolean cancel) 在 E:\ihorn_mesh_tool\usb_mesh_tools\usb_mesh_tool\LibWinUsb\Internal\OverlappedTransferContext.cs 中: 第 97 行
   在 LibUsbDotNet.Main.UsbTransfer.Wait(Int32& transferredCount) 在 E:\ihorn_mesh_tool\usb_mesh_tools\usb_mesh_tool\LibWinUsb\Main\UsbTransfer.cs 中: 第 178 行
   在 LibUsbDotNet.Main.UsbTransfer.Dispose() 在 E:\ihorn_mesh_tool\usb_mesh_tools\usb_mesh_tool\LibWinUsb\Main\UsbTransfer.cs 中: 第 132 行
   在 LibUsbDotNet.Main.UsbTransfer.Finalize() 在 E:\ihorn_mesh_tool\usb_mesh_tools\usb_mesh_tool\LibWinUsb\Main\UsbTransfer.cs 中: 第 139 行

https://stackoverflow.com/questions/20841117/libusbdotnet-error-on-exiting-wpf-application-safe-handle-has-been-closed?r=SearchResults

 

重新修改库接口如下:

public override bool GetOverlappedResult(SafeHandle interfaceHandle, IntPtr pOverlapped, out int numberOfBytesTransferred, bool wait)
{
    // To prevent ObjectDisposedException check if SafeHandle's been closed
    if (!interfaceHandle.IsClosed)
        return Kernel32.GetOverlappedResult(interfaceHandle, pOverlapped, out numberOfBytesTransferred, wait);
    else
    {
        numberOfBytesTransferred = 0;
        return true;
    }
}

 

你可能感兴趣的:(c#上位机编程)