Example #1
0
        public WinUsbDevice(String path, WinUsbRegistry registry)
        {
            this.Registry = registry;
            DeviceHandle  = Kernel32.CreateFile(path,
                                                NativeFileAccess.FILE_GENERIC_WRITE | NativeFileAccess.FILE_GENERIC_READ,
                                                NativeFileShare.FILE_SHARE_WRITE | NativeFileShare.FILE_SHARE_READ,
                                                IntPtr.Zero,
                                                NativeFileMode.OPEN_EXISTING,
                                                NativeFileFlag.FILE_ATTRIBUTE_NORMAL | NativeFileFlag.FILE_FLAG_OVERLAPPED,
                                                IntPtr.Zero);
            if (DeviceHandle.IsInvalid || DeviceHandle.IsClosed)
            {
                throw new Win32Exception();
            }
            ThreadPool.BindHandle(DeviceHandle);
            SafeWinUsbInterfaceHandle InterfaceHandle;

            if (!WinUsb_Initialize(DeviceHandle, out InterfaceHandle))
            {
                throw new Win32Exception();
            }
            if (InterfaceHandle.IsInvalid || InterfaceHandle.IsClosed)
            {
                throw new Win32Exception();
            }
            InterfaceHandles = new SafeWinUsbInterfaceHandle[1] {
                InterfaceHandle
            };
            foreach (UsbInterfaceInfo ifinfo in UsbDeviceInfo.FromDevice(this).FindConfiguration(Configuration).Interfaces)
            {
                foreach (UsbEndpointDescriptor epinfo in ifinfo.Endpoints)
                {
                    int epidx = epinfo.EndpointAddress & 0x7F;
                    if ((epinfo.EndpointAddress & 0x80) != 0)
                    {
                        if (EndpointToInterfaceIn.Length <= epidx)
                        {
                            Array.Resize(ref EndpointToInterfaceIn, epidx + 1);
                        }
                        EndpointToInterfaceIn[epidx] = ifinfo.Descriptor.InterfaceNumber;
                    }
                    else
                    {
                        if (EndpointToInterfaceOut.Length <= epidx)
                        {
                            Array.Resize(ref EndpointToInterfaceOut, epidx + 1);
                        }
                        EndpointToInterfaceOut[epidx] = ifinfo.Descriptor.InterfaceNumber;
                    }
                }
            }
        }
Example #2
0
        public static WinUsbRegistry GetDeviceForDeviceNode(DeviceNode device)
        {
            if (device.Service != "WinUSB")
            {
                return(null);
            }
            String[] devInterfaceGuids = device.GetCustomPropertyStringArray("DeviceInterfaceGuids");
            if (devInterfaceGuids == null || devInterfaceGuids.Length < 1)
            {
                return(null);
            }
            Guid deviceInterfaceGuid = new Guid(devInterfaceGuids[0]);

            String[] interfaces = device.GetInterfaces(deviceInterfaceGuid);
            if (interfaces == null || interfaces.Length < 1)
            {
                return(null);
            }
            WinUsbRegistry regInfo = new WinUsbRegistry(device, interfaces[0]);

            regInfo.DeviceInterfaceGuids = Array.ConvertAll(devInterfaceGuids, delegate(String g) { return(new Guid(g)); });
            return(regInfo);
        }