Mercurial > hg > ucis.core
changeset 93:d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 22 Apr 2014 16:40:35 +0200 |
parents | ac0a29c05d03 |
children | 3c1bba376dca |
files | USBLib/Communication/LibUsb0/LibUsbDevice.cs USBLib/Internal/Windows/SetupApi.cs USBLib/Windows/Devices/DeviceNode.cs |
diffstat | 3 files changed, 18 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- 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; } }
--- 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;
--- 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<DeviceNode> 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; + } + } } }