# HG changeset patch # User Ivo Smits # Date 1398177635 -7200 # Node ID d467cd38b34eac24ab6f6694383c567ef371ba16 # Parent ac0a29c05d03f2f226bf9ef3a485050f5c9c25dd USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function diff -r ac0a29c05d03 -r d467cd38b34e USBLib/Communication/LibUsb0/LibUsbDevice.cs --- a/USBLib/Communication/LibUsb0/LibUsbDevice.cs Fri Apr 04 23:21:33 2014 +0200 +++ b/USBLib/Communication/LibUsb0/LibUsbDevice.cs Tue Apr 22 16:40:35 2014 +0200 @@ -204,7 +204,7 @@ int transfered = 0; while (length > 0) { int ret; - DeviceIoControl(DeviceHandle, cltCode, ref req, LibUsbRequest.Size, (IntPtr)(b + offset), Math.Min(Int16.MaxValue, length), out ret); + DeviceIoControl(DeviceHandle, cltCode, ref req, LibUsbRequest.Size, (IntPtr)(b + offset), length, out ret); if (ret <= 0) throw new System.IO.EndOfStreamException(); length -= ret; offset += ret; @@ -214,7 +214,7 @@ } else { int cltCode = isochronous ? LibUsbIoCtl.ISOCHRONOUS_READ : LibUsbIoCtl.INTERRUPT_OR_BULK_READ; int ret; - DeviceIoControl(DeviceHandle, cltCode, ref req, LibUsbRequest.Size, (IntPtr)(b + offset), Math.Min(UInt16.MaxValue, length), out ret); + DeviceIoControl(DeviceHandle, cltCode, ref req, LibUsbRequest.Size, (IntPtr)(b + offset), length, out ret); return ret; } } diff -r ac0a29c05d03 -r d467cd38b34e USBLib/Internal/Windows/SetupApi.cs --- a/USBLib/Internal/Windows/SetupApi.cs Fri Apr 04 23:21:33 2014 +0200 +++ b/USBLib/Internal/Windows/SetupApi.cs Tue Apr 22 16:40:35 2014 +0200 @@ -94,6 +94,9 @@ [DllImport("setupapi.dll", CharSet = CharSet.Auto)] public static extern CR CM_Reenumerate_DevNode(UInt32 dnDevInst, UInt32 ulFlags); + [DllImport("newdev.dll", SetLastError = true, CharSet = CharSet.Auto)] + public static extern bool DiUninstallDevice(IntPtr hwndParent, SafeDeviceInfoSetHandle DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, UInt32 Flags, out Boolean NeedReboot); + //public const int DIGCF_DEFAULT = 0x00000001; // only valid with DIGCF_DEVICEINTERFACE public const int DIGCF_PRESENT = 0x00000002; public const int DIGCF_ALLCLASSES = 0x00000004; diff -r ac0a29c05d03 -r d467cd38b34e USBLib/Windows/Devices/DeviceNode.cs --- a/USBLib/Windows/Devices/DeviceNode.cs Fri Apr 04 23:21:33 2014 +0200 +++ b/USBLib/Windows/Devices/DeviceNode.cs Tue Apr 22 16:40:35 2014 +0200 @@ -59,7 +59,6 @@ } public static IList GetDevices(String enumerator, Boolean present) { using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, enumerator, IntPtr.Zero, (present ? DICFG.PRESENT : 0) | DICFG.ALLCLASSES)) { - //using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, enumerator, IntPtr.Zero, DICFG.ALLCLASSES | DICFG.DEVICEINTERFACE)) { return GetDevicesInSet(dis); } } @@ -89,7 +88,6 @@ SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) return null; - //throw new Win32Exception(Marshal.GetLastWin32Error()); RegistryValueKind propertyType; byte[] propBuffer = new byte[256]; int requiredSize; @@ -149,7 +147,6 @@ SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) return null; - //throw new Win32Exception(Marshal.GetLastWin32Error()); RegistryValueKind propertyType; byte[] propBuffer = new byte[256]; int requiredSize; @@ -341,5 +338,18 @@ throw new Win32Exception(Marshal.GetLastWin32Error()); } } + + public Boolean Uninstall() { + using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { + if (dis.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); + SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); + if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) + throw new Win32Exception(Marshal.GetLastWin32Error()); + Boolean needsReboot; + if (!SetupApi.DiUninstallDevice(IntPtr.Zero, dis, ref dd, 0, out needsReboot)) + throw new Win32Exception(Marshal.GetLastWin32Error()); + return needsReboot; + } + } } }